Generalized common functionality of different Memories to new interface
[Mograsim.git] / net.mograsim.machine / src / net / mograsim / machine / mi / parameters / MnemonicFamily.java
1 package net.mograsim.machine.mi.parameters;
2
3 import java.util.Arrays;
4 import java.util.Map;
5 import java.util.stream.Collectors;
6
7 import net.mograsim.machine.mi.parameters.MicroInstructionParameter.ParameterType;
8
9 public class MnemonicFamily implements ParameterClassification
10 {
11         private final Mnemonic[] values;
12         private final Map<String, Mnemonic> byText;
13         private final int vectorLength;
14         
15         public MnemonicFamily(Mnemonic... values)
16         {
17                 this.values = values;
18                 if(values.length == 0)
19                         vectorLength = 0;
20                 else
21                 {
22                         vectorLength = values[0].getValue().width();
23                         for(int i = 1; i < values.length; i++)
24                                 if(values[i].getValue().width() != vectorLength)
25                                         throw new IllegalArgumentException("MnemonicFamily is not of uniform vector length!");
26                 }
27                 byText = Arrays.stream(values).collect(Collectors.toMap(m -> m.getText(), m -> m));
28                 if(values.length != byText.keySet().size())
29                         throw new IllegalArgumentException("MnemonicFamily contains multiple Mnemonics with the same name!");
30         }
31         
32         public Mnemonic[] getValues()
33         {
34                 return values.clone();
35         }
36         
37         public Mnemonic get(String text)
38         {
39                 return byText.get(text);
40         }
41         
42         public boolean contains(Mnemonic m)
43         {
44                 if(m != null)
45                         return m.equals(byText.get(m.getText()));
46                 else
47                         return false;
48         }
49         
50         public boolean contains(String value)
51         {
52                 return byText.keySet().contains(value);
53         }
54         
55         public int size()
56         {
57                 return values.length;
58         }
59         
60         public int getVectorLength()
61         {
62                 return vectorLength;
63         }
64
65         @Override
66         public boolean conforms(MicroInstructionParameter param)
67         {
68                 return ParameterClassification.super.conforms(param) && (param instanceof Mnemonic ? contains((Mnemonic) param) : false);
69         }
70
71         @Override
72         public ParameterType getExpectedType()
73         {
74                 return ParameterType.MNEMONIC;
75         }
76
77         @Override
78         public int getExpectedBits()
79         {
80                 return vectorLength;
81         }
82
83         public String[] getStringValues()
84         {
85                 return byText.keySet().toArray(new String[values.length]);
86         }
87 }