From 9f37139ab35cc63a2940ee6b67692632867c8da7 Mon Sep 17 00:00:00 2001 From: Florian Steurer Date: Fri, 13 Nov 2015 22:45:13 +0100 Subject: [PATCH] real immutability --- .../typinference/unify/model/ExtendsType.java | 3 ++- .../unify/model/PlaceholderType.java | 4 ++-- .../typinference/unify/model/SimpleType.java | 5 ++--- .../typinference/unify/model/SuperType.java | 3 ++- .../typinference/unify/model/Type.java | 15 ++++++++----- .../typinference/unify/model/TypeParams.java | 22 +++++++++++++++---- 6 files changed, 36 insertions(+), 16 deletions(-) diff --git a/src/de/dhbwstuttgart/typinference/unify/model/ExtendsType.java b/src/de/dhbwstuttgart/typinference/unify/model/ExtendsType.java index c01ff5d4..d648c249 100644 --- a/src/de/dhbwstuttgart/typinference/unify/model/ExtendsType.java +++ b/src/de/dhbwstuttgart/typinference/unify/model/ExtendsType.java @@ -4,10 +4,11 @@ import java.util.Set; import de.dhbwstuttgart.typeinference.unify.interfaces.IFiniteClosure; -public class ExtendsType extends Type { +public final class ExtendsType extends Type { private Type extendedType; public ExtendsType(Type extendedType) { + super("? extends " + extendedType.getTypeParams(), extendedType.getTypeParams()); this.extendedType = extendedType; } diff --git a/src/de/dhbwstuttgart/typinference/unify/model/PlaceholderType.java b/src/de/dhbwstuttgart/typinference/unify/model/PlaceholderType.java index 48cf2e6f..dd40ab23 100644 --- a/src/de/dhbwstuttgart/typinference/unify/model/PlaceholderType.java +++ b/src/de/dhbwstuttgart/typinference/unify/model/PlaceholderType.java @@ -4,10 +4,10 @@ import java.util.Set; import de.dhbwstuttgart.typeinference.unify.interfaces.IFiniteClosure; -public class PlaceholderType extends Type{ +public final class PlaceholderType extends Type{ public PlaceholderType(String name) { - typeName = name; + super(name); } @Override diff --git a/src/de/dhbwstuttgart/typinference/unify/model/SimpleType.java b/src/de/dhbwstuttgart/typinference/unify/model/SimpleType.java index 199c6518..29228977 100644 --- a/src/de/dhbwstuttgart/typinference/unify/model/SimpleType.java +++ b/src/de/dhbwstuttgart/typinference/unify/model/SimpleType.java @@ -4,10 +4,9 @@ import java.util.Set; import de.dhbwstuttgart.typeinference.unify.interfaces.IFiniteClosure; -public class SimpleType extends Type { +public final class SimpleType extends Type { public SimpleType(String name, Type... typeParams) { - this.typeName = name; - this.typeParams = new TypeParams(typeParams); + super(name, new TypeParams(typeParams)); } @Override diff --git a/src/de/dhbwstuttgart/typinference/unify/model/SuperType.java b/src/de/dhbwstuttgart/typinference/unify/model/SuperType.java index 5a734621..2148e0df 100644 --- a/src/de/dhbwstuttgart/typinference/unify/model/SuperType.java +++ b/src/de/dhbwstuttgart/typinference/unify/model/SuperType.java @@ -4,11 +4,12 @@ import java.util.Set; import de.dhbwstuttgart.typeinference.unify.interfaces.IFiniteClosure; -public class SuperType extends Type { +public final class SuperType extends Type { private Type superedType; public SuperType(Type superedType) { + super("? extends " + superedType.getName(), superedType.getTypeParams()); this.superedType = superedType; } diff --git a/src/de/dhbwstuttgart/typinference/unify/model/Type.java b/src/de/dhbwstuttgart/typinference/unify/model/Type.java index dc5a9d91..3da9dbcd 100644 --- a/src/de/dhbwstuttgart/typinference/unify/model/Type.java +++ b/src/de/dhbwstuttgart/typinference/unify/model/Type.java @@ -1,17 +1,22 @@ package de.dhbwstuttgart.typinference.unify.model; -import java.util.Arrays; import java.util.Set; import de.dhbwstuttgart.typeinference.unify.interfaces.IFiniteClosure; public abstract class Type { - protected String typeName = ""; - protected TypeParams typeParams; + protected final String typeName; + protected final TypeParams typeParams; - public Type() { - typeParams = new TypeParams(); + protected Type(String name, Type... typeParams) { + typeName = name; + this.typeParams = new TypeParams(typeParams); + } + + protected Type(String name, TypeParams p) { + typeName = name; + typeParams = p; } public String getName() { diff --git a/src/de/dhbwstuttgart/typinference/unify/model/TypeParams.java b/src/de/dhbwstuttgart/typinference/unify/model/TypeParams.java index c89b207d..c6e3ee00 100644 --- a/src/de/dhbwstuttgart/typinference/unify/model/TypeParams.java +++ b/src/de/dhbwstuttgart/typinference/unify/model/TypeParams.java @@ -3,8 +3,10 @@ package de.dhbwstuttgart.typinference.unify.model; import java.util.Arrays; import java.util.Iterator; -public class TypeParams implements Iterable{ - private Type[] typeParams; +import de.dhbwstuttgart.typeinference.unifynew.Unifier; + +public final class TypeParams implements Iterable{ + private final Type[] typeParams; public TypeParams(Type... types) { typeParams = types; @@ -26,8 +28,20 @@ public class TypeParams implements Iterable{ return typeParams.length == 0; } - public Type[] asArray() { - return typeParams; + public void apply(Unifier u) { + for(int i = 0; i < typeParams.length; i++) + typeParams[i] = u.apply(typeParams[i]); + } + + public boolean contains(Type t) { + for(Type t1 : typeParams) + if(t1.equals(t1)) + return true; + return false; + } + + public Type get(int i) { + return typeParams[i]; } @Override