TypeCombined improvements

This commit is contained in:
BuildTools 2016-02-11 17:32:04 +01:00 committed by Olof Larsson
parent ecd30dfa96
commit 78b815cc71
2 changed files with 57 additions and 22 deletions

View File

@ -24,17 +24,7 @@ public abstract class TypeCombined<T> extends TypeAbstract<T>
private Pattern separatorsPattern = null; private Pattern separatorsPattern = null;
public Pattern getSeparatorsPattern() { return this.separatorsPattern; } public Pattern getSeparatorsPattern() { return this.separatorsPattern; }
private void buildSeparatorsPattern() private void buildSeparatorsPattern() { this.separatorsPattern = buildSeparatorsPattern(this.separators); }
{
StringBuilder regex = new StringBuilder();
regex.append("[");
for (char c : this.separators.toCharArray())
{
regex.append(Pattern.quote(String.valueOf(c)));
}
regex.append("]+");
separatorsPattern = Pattern.compile(regex.toString());
}
private String separators = null; private String separators = null;
public String getSeparators() { return this.separators; } public String getSeparators() { return this.separators; }
@ -44,6 +34,37 @@ public abstract class TypeCombined<T> extends TypeAbstract<T>
this.buildSeparatorsPattern(); this.buildSeparatorsPattern();
} }
private String separatorTypeName = " ";
public String getSeparatorTypeName() { return this.separatorTypeName; }
public void setSeparatorTypeName(String separatorTypeName) { this.separatorTypeName = separatorTypeName; }
// Visual
private boolean includeNullVisual = true;
public boolean doesIncludeNullVisual() { return this.includeNullVisual; }
public void setIncludeNullVisual(boolean includeNullVisual) { this.includeNullVisual = includeNullVisual; }
private String separatorVisual = " ";
public String getSeparatorVisual() { return this.separatorVisual; }
public void setSeparatorVisual(String separatorVisual) { this.separatorVisual = separatorVisual; }
// Name
private boolean includeNullName = true;
public boolean doesIncludeNullName() { return this.includeNullName; }
public void setIncludeNullName(boolean includeNullName) { this.includeNullName = includeNullName; }
private String separatorName = " ";
public String getSeparatorName() { return this.separatorName; }
public void setSeparatorName(String separatorName) { this.separatorName = separatorName; }
// Id
private boolean includeNullId = true;
public boolean doesIncludeNullId() { return this.includeNullId; }
public void setIncludeNullId(boolean includeNullId) { this.includeNullId = includeNullId; }
private String separatorId = " ";
public String getSeparatorId() { return this.separatorId; }
public void setSeparatorId(String separatorId) { this.separatorId = separatorId; }
// -------------------------------------------- // // -------------------------------------------- //
// CONSTRUCT // CONSTRUCT
// -------------------------------------------- // // -------------------------------------------- //
@ -103,7 +124,7 @@ public abstract class TypeCombined<T> extends TypeAbstract<T>
} }
// Return // Return
return Txt.implode(parts, " "); return Txt.implode(parts, this.getSeparatorTypeName());
} }
// -------------------------------------------- // // -------------------------------------------- //
@ -122,11 +143,12 @@ public abstract class TypeCombined<T> extends TypeAbstract<T>
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
Type<Object> type = (Type<Object>) entry.getKey(); Type<Object> type = (Type<Object>) entry.getKey();
String part = type.getVisual(entry.getValue(), sender); String part = type.getVisual(entry.getValue(), sender);
if ( ! this.doesIncludeNullVisual() && part == null) continue;
parts.add(part); parts.add(part);
} }
// Return // Return
return Txt.implode(parts, " "); return Txt.implode(parts, this.getSeparatorVisual());
} }
// -------------------------------------------- // // -------------------------------------------- //
@ -145,11 +167,12 @@ public abstract class TypeCombined<T> extends TypeAbstract<T>
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
Type<Object> type = (Type<Object>) entry.getKey(); Type<Object> type = (Type<Object>) entry.getKey();
String part = type.getName(entry.getValue()); String part = type.getName(entry.getValue());
if ( ! this.doesIncludeNullName() && part == null) continue;
parts.add(part); parts.add(part);
} }
// Return // Return
return Txt.implode(parts, " "); return Txt.implode(parts, this.getSeparatorName());
} }
// -------------------------------------------- // // -------------------------------------------- //
@ -168,11 +191,12 @@ public abstract class TypeCombined<T> extends TypeAbstract<T>
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
Type<Object> type = (Type<Object>) entry.getKey(); Type<Object> type = (Type<Object>) entry.getKey();
String part = type.getId(entry.getValue()); String part = type.getId(entry.getValue());
if ( ! this.doesIncludeNullId() && part == null) continue;
parts.add(part); parts.add(part);
} }
// Return // Return
return Txt.implode(parts, " "); return Txt.implode(parts, this.getSeparatorId());
} }
// -------------------------------------------- // // -------------------------------------------- //
@ -257,5 +281,21 @@ public abstract class TypeCombined<T> extends TypeAbstract<T>
if (args.size() > this.getInnerTypes().size()) return null; if (args.size() > this.getInnerTypes().size()) return null;
return this.getInnerType(args.size() - 1); return this.getInnerType(args.size() - 1);
} }
// -------------------------------------------- //
// UTIL
// -------------------------------------------- //
public static Pattern buildSeparatorsPattern(String separators)
{
StringBuilder regex = new StringBuilder();
regex.append("[");
for (char c : separators.toCharArray())
{
regex.append(Pattern.quote(String.valueOf(c)));
}
regex.append("]+");
return Pattern.compile(regex.toString());
}
} }

View File

@ -24,6 +24,7 @@ public class TypeEntry<K, V> extends TypeCombined<Entry<K, V>>
public TypeEntry(Type<K> keyType, Type<V> valueType) public TypeEntry(Type<K> keyType, Type<V> valueType)
{ {
super(keyType, valueType); super(keyType, valueType);
this.setSeparatorTypeName(" and ");
} }
// -------------------------------------------- // // -------------------------------------------- //
@ -36,13 +37,7 @@ public class TypeEntry<K, V> extends TypeCombined<Entry<K, V>>
// -------------------------------------------- // // -------------------------------------------- //
// OVERRIDE // OVERRIDE
// -------------------------------------------- // // -------------------------------------------- //
@Override
public String getTypeName()
{
return this.getKeyType().getTypeName() + " and " + this.getValueType().getTypeName();
}
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@Override @Override
public Entry<K, V> combine(List<Object> parts) public Entry<K, V> combine(List<Object> parts)