Package cdc.args
Class FormalArg<T>
- java.lang.Object
-
- cdc.args.FormalArg<T>
-
- Type Parameters:
T- Type of the formal argument.
- Direct Known Subclasses:
CompositeFormalArg,MapFormalArg
public class FormalArg<T> extends Object
Class used to describe a formal argument.A formal argument has a name, a type and may be optional or mandatory.
- Author:
- Damien Carbonne
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classFormalArg.Builder<T>
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static <T> FormalArg.Builder<T>builder(Class<T> type)static voidcheckName(String name)Checks a name.booleanequals(Object other)StringgetDescription()StringgetName()NecessitygetNecessity()Class<T>getType()Class<T>getWrappedType()inthashCode()booleanhasWeakerNecessity(Necessity other)Returnstrueif the necessity of this formal argument is weaker than (or equal to) another necessity.booleanhasWeakerType(Class<?> other)Returnstrueif the (wrapped) type of this formal argument can be assigned by another type.booleanisCompliantWith(Object value)Returns whether a value is compliant with this formal argument.booleanisMandatory()booleanisOptional()static booleanisValidName(String s)Returns true when a string can be used as a valid name.booleanisWeakerThan(FormalArg<?> other)Returnstrueif this formal argument is weaker than another one.booleanmatchesName(String other)Returnstrueif the name of this formal argument equals another name.static FormalArg<?>merge(FormalArg<?> farg1, FormalArg<?> farg2, Necessity privileged)Merges 2 formal arguments.StringtoString()
-
-
-
Constructor Detail
-
FormalArg
public FormalArg(String name, Class<T> type, Necessity necessity)
Creates a formal argument.- Parameters:
name- The formal argument name.type- The formal argument type.necessity- The formal argument necessity.- Throws:
IllegalArgumentException- whennameis invalid, or whentypeornecessityisnull.
-
FormalArg
public FormalArg(String name, Class<T> type)
Creates a MANDATORY formal argument.- Parameters:
name- The formal argument name.type- The formal argument type.- Throws:
IllegalArgumentException- whennameis invalid, or whentypeisnull.
-
-
Method Detail
-
isValidName
public static boolean isValidName(String s)
Returns true when a string can be used as a valid name.Rules are very relaxed: only
nullnames are forbidden.
Stronger rules should be applied.- Parameters:
s- The string- Returns:
- True when
sis a valid name.
-
checkName
public static void checkName(String name)
Checks a name.- Parameters:
name- The name- Throws:
IllegalArgumentException- whennameis invalid.
-
getName
public final String getName()
- Returns:
- The formal argument name.
-
getDescription
public final String getDescription()
- Returns:
- The formal argument description.
-
getNecessity
public final Necessity getNecessity()
- Returns:
- The formal argument necessity.
-
isMandatory
public final boolean isMandatory()
- Returns:
trueif this formal argument is mandatory.
-
isOptional
public final boolean isOptional()
- Returns:
trueif this formal argument is optional.
-
isCompliantWith
public final boolean isCompliantWith(Object value)
Returns whether a value is compliant with this formal argument.If
valueis null, then this formal argument must be optional.
Otherwise,valuemust be assignable towrappedTypeof the formal argument.- Parameters:
value- The value to check.- Returns:
- Whether
valueis compliant with this formal argument.
-
matchesName
public final boolean matchesName(String other)
Returnstrueif the name of this formal argument equals another name.- Parameters:
other- The other name.- Returns:
trueif the name of this formal argument equalsother.
-
hasWeakerType
public final boolean hasWeakerType(Class<?> other)
Returnstrueif the (wrapped) type of this formal argument can be assigned by another type.- Parameters:
other- The other type.- Returns:
trueif the (wrapped) type of this formal argument can be assigned byother.
-
hasWeakerNecessity
public final boolean hasWeakerNecessity(Necessity other)
Returnstrueif the necessity of this formal argument is weaker than (or equal to) another necessity.- Parameters:
other- The other necessity.- Returns:
trueif the necessity of this formal argument is weaker than (or equal to)other.
-
isWeakerThan
public final boolean isWeakerThan(FormalArg<?> other)
Returnstrueif this formal argument is weaker than another one.This is the case when any argument that is compliant with
otheris compliant with this formal argument.It must have:
- The same name.
- A weaker necessity.
- A weaker (more general) type.
- Parameters:
other- The other formal argument.- Returns:
trueif this formal argument is weaker thanother.
-
merge
public static FormalArg<?> merge(FormalArg<?> farg1, FormalArg<?> farg2, Necessity privileged)
Merges 2 formal arguments.This is possible if one of them is
null, or they have same name and compliant types.
In that case, if necessity are different, the resulting necessity is MANDATORY.
When types are compliant, the most specialized one is chosen.For example:
merge(null, null) = nullmerge(null, w) = xmerge(x, null) = xmerge((name1, ...), (name2, ...)) throws IllegalArgumentExceptionmerge((name, String.class, OPTIONAL), (name, Object, OPTIONAL)) = (name, String.class, OPTIONAL)merge((name, String.class, OPTIONAL), (name, Object, MANDATORY)) = (name, String.class, MANDATORY)merge((name, String.class, OPTIONAL), (name, Integer, OPTIONAL)) = throws IllegalArgumentException
- Parameters:
farg1- The first formal argument (possiblynull).farg2- The second formal argument (possiblynull).privileged- The privileged necessity.- Returns:
- The merge of
farg1andfarg2. - Throws:
IllegalArgumentException- When merge is impossible (names conflict or types conflict).
-
builder
public static <T> FormalArg.Builder<T> builder(Class<T> type)
-
-