この場合を考えてみましょう。
いかなる方法でも変更または拡張できないクラスがあります。
public class Foo {
...
private Boolean bar;
...
}
を介してそのクラスのフィールドを編集する必要がありますが、そのクラスBeanEditor
の背後にあるロジックでは、null、true、およびfalseBoolean
の3 つの状態を持つことができるという事実が許可され、使用されます。
ただし、Tapestry はtrueまたはfalseの 2 つのオプションしかないチェックボックスを提供します。
そのため、オンラインの人々は、タイプ プロパティを 3 方向ロジックを表すタイプBoolean
プロパティに変換することを提案しています。BooleanExtendedEnum
public enum BooleanExtendedEnum {
UNDEFINED(null),
TRUE(Boolean.TRUE),
FALSE(Boolean.FALSE);
private Boolean booleanValue;
private static Map<Boolean, BooleanExtendedEnum> booleanToExtendedMap = new HashMap<Boolean, BooleanExtendedEnum>();
static {
for (BooleanExtendedEnum be : BooleanExtendedEnum.values()) {
booleanToExtendedMap.put(be.booleanValue, be);
}
}
private BooleanExtendedEnum(Boolean booleanValue) {
this.booleanValue = booleanValue;
}
public Boolean getBooleanValue() {
return booleanValue;
}
public static BooleanExtendedEnum getBooleanExtendedValue(Boolean booleanInput) {
return booleanToExtendedMap.get(booleanInput);
}
}
クラスを変更することはできないFoo
ため、 の coercer を作成する必要がありますBoolean <=> BooleanExtendedEnum
。
Coercion<Boolean, BooleanExtendedEnum> threeWayBooleanToExtended = new Coercion<Boolean, BooleanExtendedEnum>() {
@Override
public BooleanExtendedEnum coerce(Boolean input) {
if (input == null) {
return BooleanExtendedEnum.UNDEFINED;
} else {
return BooleanExtendedEnum.getBooleanExtendedEnumValue(input);
}
}
};
Coercion<BooleanExtendedEnum, Boolean> threeWayExtendedToBoolean = new Coercion<BooleanExtendedEnum, Boolean>() {
@Override
public Boolean coerce(BooleanExtendedEnum input) {
if (input == null) {
return null;
} else {
return input.getBooleanValue();
}
}
};
configuration.add(new CoercionTuple<Boolean, BooleanExtendedEnum>(Boolean.class, BooleanExtendedEnum.class, threeWayBooleanToExtended));
configuration.add(new CoercionTuple<BooleanExtendedEnum, Boolean>(BooleanExtendedEnum.class, Boolean.class, threeWayExtendedToBoolean));
BeanEditor
で次のような簡単なことをしたとしましょうtml
:
<p:bar>
<div class="t-beaneditor-row">
<label>Bar Value</label>
<t:select t:id="fooBar" t:value="foo.bar" t:model="booleanExtendedSelectModel" t:blankOption="NEVER"/>
</div>
</p:bar>
...そして次のSelectModel
ようなものを提供しました:
public SelectModel getBooleanExtendedSelectModel() {
return new EnumSelectModel(BooleanExtendedEnum.class, messages);
}
Tapestry は 3 つのオプションを含むドロップダウン リストを作成します
Undefined
True
False
ただし、表示された値を強制する実際のブール値は次のようになります。
Undefined
->真True
->真False
->偽
クラスを変更したり、型フィールドが型フィールドに置き換えられた別のクラスにラップしたり、他の「ハッキー」ソリューションを使用したりしないという制限付きで、目的の効果 ( Undefined
-> null ) をどのように達成できますか?Boolean
BooleanExtendedEnum