Compilation of SCL expressions from SCL
[simantics/platform.git] / bundles / org.simantics.scl.compiler / src / org / simantics / scl / compiler / environment / specification / EnvironmentSpecification.java
1 package org.simantics.scl.compiler.environment.specification;
2
3 import java.util.ArrayList;
4 import java.util.List;
5
6 import org.simantics.scl.compiler.module.ImportDeclaration;
7 import org.simantics.scl.runtime.tuple.Tuple2;
8
9 public class EnvironmentSpecification {
10     public final ArrayList<ImportDeclaration> imports = new ArrayList<ImportDeclaration>();
11     
12     public void importModule(String moduleName, String localName) {
13         imports.add(new ImportDeclaration(moduleName, localName));
14     }
15     
16     public static EnvironmentSpecification of(String ... strings) {
17         if(strings.length % 2 != 0)
18             throw new IllegalArgumentException("Expected even number of parameters.");
19         EnvironmentSpecification spec = new EnvironmentSpecification();
20         for(int i=0;i<strings.length;i+=2)
21             spec.importModule(strings[i], strings[i+1]);
22         return spec;
23     }
24     
25     public static EnvironmentSpecification fromList(List<Tuple2> imports) {
26         EnvironmentSpecification spec = new EnvironmentSpecification();
27         for(Tuple2 tuple : imports)
28             spec.importModule((String)tuple.c0, (String)tuple.c1);
29         return spec;
30     }
31
32     @Override
33     public int hashCode() {
34         final int prime = 31;
35         int result = 1;
36         result = prime * result + ((imports == null) ? 0 : imports.hashCode());
37         return result;
38     }
39
40     @Override
41     public boolean equals(Object obj) {
42         if (this == obj)
43             return true;
44         if (obj == null)
45             return false;
46         if (getClass() != obj.getClass())
47             return false;
48         EnvironmentSpecification other = (EnvironmentSpecification) obj;
49         if (imports == null) {
50             if (other.imports != null)
51                 return false;
52         } else if (!imports.equals(other.imports))
53             return false;
54         return true;
55     }
56 }