| 1 | package uk.co.zonetora.fj.model; |
| 2 | |
| 3 | import java.util.ArrayList; |
| 4 | import java.util.HashSet; |
| 5 | import java.util.List; |
| 6 | import java.util.Set; |
| 7 | |
| 8 | import uk.co.zonetora.fj.passes.FJException; |
| 9 | import uk.co.zonetora.fj.util.Tuple; |
| 10 | import static uk.co.zonetora.fj.util.ListUtil.*; |
| 11 | |
| 12 | |
| 13 | |
| 14 | public class Constructor { |
| 15 | |
| 16 | private final ClassName returnType; |
| 17 | private final List<Tuple<ClassName, FieldName>> arguments; |
| 18 | |
| 19 | private final List<FieldName> superFields; |
| 20 | private final List<FieldName> localFields; |
| 21 | |
| 22 | public Constructor(ClassName returnType) { |
| 23 | this.returnType = returnType; |
| 24 | this.superFields = new ArrayList<FieldName>(); |
| 25 | this.localFields = new ArrayList<FieldName>(); |
| 26 | this.arguments = new ArrayList<Tuple<ClassName,FieldName>>(); |
| 27 | } |
| 28 | |
| 29 | public void addSuperField(FieldName fn) { |
| 30 | this.superFields.add(fn); |
| 31 | } |
| 32 | |
| 33 | public void addLocalField(FieldName fn) { |
| 34 | this.localFields.add(fn); |
| 35 | } |
| 36 | |
| 37 | public void addArgument(ClassName cn, FieldName fn) { |
| 38 | this.arguments.add(new Tuple<ClassName, FieldName>(cn,fn)); |
| 39 | } |
| 40 | |
| 41 | public Set<ClassName> getAllReferencedClassNames() { |
| 42 | Set<ClassName> allReferencedClassNames = new HashSet<ClassName>(); |
| 43 | allReferencedClassNames.add(returnType); |
| 44 | |
| 45 | for(Tuple<ClassName, FieldName> arg : arguments) { |
| 46 | allReferencedClassNames.add(arg.getX()); |
| 47 | } |
| 48 | |
| 49 | return allReferencedClassNames; |
| 50 | } |
| 51 | |
| 52 | public void checkFieldsAreSane(List<Tuple<ClassName, FieldName>> fields, |
| 53 | List<Tuple<ClassName, FieldName>> superFields) throws FJException { |
| 54 | List<Tuple<ClassName, FieldName>> allFields = new ArrayList<Tuple<ClassName,FieldName>>(); |
| 55 | allFields.addAll(superFields); |
| 56 | allFields.addAll(fields); |
| 57 | |
| 58 | if(!arguments.equals(allFields)) { |
| 59 | throw new FJException("Constructor fields do not match parent class"); |
| 60 | } |
| 61 | |
| 62 | if(!(mapFst(superFields).equals(this.superFields))){ |
| 63 | throw new FJException("Superfields in constructor do not match up"); |
| 64 | } |
| 65 | |
| 66 | if(!(mapSnd(fields).equals(this.localFields))) { |
| 67 | throw new FJException("Localfields in constructor do not mach up"); |
| 68 | } |
| 69 | |
| 70 | } |
| 71 | |
| 72 | public Object getReturnClassName() { |
| 73 | return returnType; |
| 74 | } |
| 75 | } |
| 76 | |