Defined Interfaces regarding MicroInstructions
[Mograsim.git] / net.mograsim.machine / src / net / mograsim / machine / mnemonics / MnemonicFamily.java
1 package net.mograsim.machine.mnemonics;
2
3 import java.util.Arrays;
4 import java.util.Map;
5 import java.util.stream.Collectors;
6
7 public class MnemonicFamily
8 {
9         private final Mnemonic[] values;
10         private final Map<String, Mnemonic> byText;
11         private final int vectorLenght;
12         
13         public MnemonicFamily(Mnemonic... values)
14         {
15                 this.values = values;
16                 if(values.length == 0)
17                         vectorLenght = 0;
18                 else
19                 {
20                         vectorLenght = values[0].getVector().width();
21                         for(int i = 1; i < values.length; i++)
22                                 if(values[i].getVector().width() != vectorLenght)
23                                         throw new IllegalArgumentException("MnemonicFamily is not of uniform vector length!");
24                 }
25                 byText = Arrays.stream(values).collect(Collectors.toMap(m -> m.getText(), m -> m));
26         }
27         
28         public Mnemonic[] getValues()
29         {
30                 return values.clone();
31         }
32         
33         public Mnemonic get(String text)
34         {
35                 return byText.get(text);
36         }
37         
38         public boolean contains(Mnemonic m)
39         {
40                 if(m != null)
41                         return m.equals(byText.get(m.getText()));
42                 else
43                         return false;
44         }
45         
46         public int size()
47         {
48                 return values.length;
49         }
50         
51         public int getVectorLength()
52         {
53                 return vectorLenght;
54         }
55 }