]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/parser/generator/table/ItemSet.java
Moved SCL parser generator to platform repository.
[simantics/platform.git] / bundles / org.simantics.scl.compiler / src / org / simantics / scl / compiler / parser / generator / table / ItemSet.java
1 package org.simantics.scl.compiler.parser.generator.table;
2
3 import java.util.Arrays;
4 import java.util.Collection;
5
6 import org.simantics.scl.compiler.parser.generator.grammar.AnaGrammar;
7
8 public class ItemSet {
9     public final Item[] items;
10     int hash;
11
12     public ItemSet(Item[] items) {
13         this.items = items;
14         Arrays.sort(items);
15     }
16     
17     public ItemSet(Collection<Item> items) {
18         this(items.toArray(new Item[items.size()]));
19     }
20     
21     private static final int PROD = 31*31;
22     
23     @Override
24     public int hashCode() {
25         if(hash == 0) {
26             int h = 1;
27             for(Item item : items)
28                 h = PROD*h + item.hashCode();
29             hash = h;
30         }
31         return hash;
32     }
33     
34     @Override
35     public boolean equals(Object obj) {
36         if(this == obj)
37             return true;
38         if(obj == null || obj.getClass() != getClass())
39             return false;
40         ItemSet other = (ItemSet)obj;
41         if(other.items.length != items.length)
42             return false;
43         for(int i=0;i<items.length;++i)
44             if(!items[i].equals(other.items[i]))
45                 return false;
46         return true;
47     }
48     
49     public String toString(AnaGrammar grammar) {
50         StringBuilder b = new StringBuilder();
51             boolean first = true;
52         for(Item item : items) {
53             if(first)
54                 first = false;
55             else
56                 b.append('\n');
57             b.append("    ").append(item.toString(grammar));
58         }
59         return b.toString();
60     }
61 }