Undo renaming of BitVector's length to width
[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 vectorLenght;
14         
15         public MnemonicFamily(Mnemonic... values)
16         {
17                 this.values = values;
18                 if(values.length == 0)
19                         vectorLenght = 0;
20                 else
21                 {
22                         vectorLenght = values[0].getValue().length();
23                         for(int i = 1; i < values.length; i++)
24                                 if(values[i].getValue().length() != vectorLenght)
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         }
29         
30         public Mnemonic[] getValues()
31         {
32                 return values.clone();
33         }
34         
35         public Mnemonic get(String text)
36         {
37                 return byText.get(text);
38         }
39         
40         public boolean contains(Mnemonic m)
41         {
42                 if(m != null)
43                         return m.equals(byText.get(m.getText()));
44                 else
45                         return false;
46         }
47         
48         public int size()
49         {
50                 return values.length;
51         }
52         
53         public int getVectorLength()
54         {
55                 return vectorLenght;
56         }
57
58         @Override
59         public boolean conforms(MicroInstructionParameter param)
60         {
61                 return param instanceof Mnemonic ? contains((Mnemonic) param) : false;
62         }
63
64         @Override
65         public ParameterType getExpectedType()
66         {
67                 return ParameterType.MNEMONIC;
68         }
69 }