From 8b6a4d174621bce3b68b8cff60be414daaaf891e Mon Sep 17 00:00:00 2001 From: Florian Steurer Date: Tue, 12 Apr 2016 11:28:07 +0200 Subject: [PATCH] refactoring and commenting --- .../unify/model/PlaceholderType.java | 52 ++++++++++++++----- .../unify/model/ReferenceType.java | 6 +++ 2 files changed, 45 insertions(+), 13 deletions(-) diff --git a/src/de/dhbwstuttgart/typeinference/unify/model/PlaceholderType.java b/src/de/dhbwstuttgart/typeinference/unify/model/PlaceholderType.java index 9b032b4a..01102fca 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/model/PlaceholderType.java +++ b/src/de/dhbwstuttgart/typeinference/unify/model/PlaceholderType.java @@ -6,42 +6,68 @@ import java.util.Set; import de.dhbwstuttgart.typeinference.unify.interfaces.IFiniteClosure; +/** + * An unbounded placeholder type. + * @author Florian Steurer + */ public final class PlaceholderType extends UnifyType{ + /** + * Static list containing the names of all existing placeholders. + * Used for generating fresh placeholders. + */ protected static final HashSet EXISTING_PLACEHOLDERS = new HashSet(); + + /** + * Prefix of auto-generated placeholder names. + */ protected static String nextName = "gen_"; + + /** + * Random number generator used to generate fresh placeholder name. + */ protected static Random rnd = new Random(43558747548978L); + /** + * True if this object was auto-generated, false if this object was user-generated. + */ private final boolean IsGenerated; + /** + * Creates a new placeholder type with the specified name. + */ public PlaceholderType(String name) { super(name, new TypeParams()); - EXISTING_PLACEHOLDERS.add(name); - IsGenerated = false; + EXISTING_PLACEHOLDERS.add(name); // Add to list of existing placeholder names + IsGenerated = false; // This type is user generated } + /** + * Creates a new placeholdertype + * @param isGenerated true if this placeholder is auto-generated, false if it is user-generated. + */ protected PlaceholderType(String name, boolean isGenerated) { super(name, new TypeParams()); - EXISTING_PLACEHOLDERS.add(name); + EXISTING_PLACEHOLDERS.add(name); // Add to list of existing placeholder names IsGenerated = isGenerated; } + /** + * Creates a fresh placeholder type with a name that does so far not exist. + * A user could later instantiate a type using the same name that is equivalent to this type. + * @return A fresh placeholder type. + */ public static PlaceholderType freshPlaceholder() { - String name = nextName + randomChar(); - + String name = nextName + (char) (rnd.nextInt(22) + 97); // Returns random char between 'a' and 'z' + // Add random chars while the name is in use. while(EXISTING_PLACEHOLDERS.contains(name)); - name += randomChar(); - + name += (char) (rnd.nextInt(22) + 97); // Returns random char between 'a' and 'z' return new PlaceholderType(name, true); } /** - * Returns random char between 'a' and 'z' + * True if this placeholder is auto-generated, false if it is user-generated. */ - private static char randomChar() { - return (char) (rnd.nextInt(22) + 97); - } - public boolean isGenerated() { return IsGenerated; } @@ -58,7 +84,7 @@ public final class PlaceholderType extends UnifyType{ @Override public UnifyType setTypeParams(TypeParams newTp) { - return this; + return this; // Placeholders never have params. } @Override diff --git a/src/de/dhbwstuttgart/typeinference/unify/model/ReferenceType.java b/src/de/dhbwstuttgart/typeinference/unify/model/ReferenceType.java index 4f557b53..ad02c9ab 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/model/ReferenceType.java +++ b/src/de/dhbwstuttgart/typeinference/unify/model/ReferenceType.java @@ -4,7 +4,13 @@ import java.util.Set; import de.dhbwstuttgart.typeinference.unify.interfaces.IFiniteClosure; +/** + * A reference type e.q. Integer or List. + * @author Florian Steurer + * + */ public final class ReferenceType extends UnifyType { + public ReferenceType(String name, UnifyType... typeParams) { super(name, new TypeParams(typeParams)); }