I have an enum property in the view-model and I want to make a label visible if the property value is of a specific value
States state;
public States State
{
get { return this.state; }
set
{
if (this.state != value)
{
this.state = value;
this.RaisePropertyChanged("State");
}
}
}
public enum States
{
Stopped,
Runnning
}
here's what I tried in the UI:
local:MvxBind="Visibility If(State==1, 'Visible', 'Gone')"
but it doesn't work. No error is displayed in the log.
I can make a converter but I was wondering if it's possible to do it without it.
If I modify the State property to use int instead of Enum, it works fine.
I guess it has to do with how currently Enum type is handled.
In the Tibet binding addons, statements like:
If(State==1, 'Visible', 'Gone'
are not compiled because of iOS JIT limitations - instead they are evaluated.
Sadly the evaluation of operators like == turned out to be quite difficult to do without using dynamic code...
Because of this, what the Tibet == operator tries to do is to reduce the right and left sides down to one of three types: long, double or object - it does this using:
private Type GetLookupTypeFor(object value)
{
if (value == null)
return null;
if (value is long)
return typeof(long);
if (value is double)
return typeof (double);
return typeof (object);
}
from https://github.com/MvvmCross/MvvmCross/blob/v3.1/Cirrious/Cirrious.MvvmCross.Binding/Combiners/MvxPairwiseValueCombiner.cs#L36
with these types determined it then calls one of these methods, each of which uses the == operator of the left-hand type or .Equals if both types are object:
protected override bool CombineDoubleAndDouble(double input1, double input2, out object value)
{
value = input1 == input2;
return true;
}
protected override bool CombineDoubleAndLong(double input1, long input2, out object value)
{
value = input1 == input2;
return true;
}
// ... etc
from https://github.com/MvvmCross/MvvmCross/blob/v3.1/Cirrious/Cirrious.MvvmCross.Binding/Combiners/VeryExperimental/MvxEqualToValueCombiner.cs
For the enum case of State==1, I think the left-hand will be identified as an object and the right-hand as a long - so these will never be equal because of:
protected override bool CombineObjectAndLong(object input1, long input2, out object value)
{
value = false;
return true;
}
Working with the current code, I think the only way to get the test to work is to get both left and right-side to long or to string.
You could do that using a value converter on the enum - e.g. a ToStringValueConverter or a ToLongValueConverter which you could then use like:
If(ToLong(State)==1, 'Visible', 'Gone'
or:
If(ToString(State)=='Running', 'Visible', 'Gone'
Obviously, looking forwards, this area could also be improved inside Mvx - in whatever lies beyond Swiss and Tibet
Related
I have a simple problem. I suggest a list of objects to the user to choose from using p:autocomplete. At the same time the user can create new objects 'on the fly' by typing names that are not in the suggestion. The whole setup is very similar to the vanilla POJO showcase, but I'm posting the code here:
<p:autoComplete id="player" value="#{entry.player}"
completeMethod="{abean.complete}"
var="s1" itemLabel="#{s1.name}" itemValue="#{s1}"
converter="PlayerConverter">
<p:ajax event="itemSelect" listener="#{abean.playerSelected}"/>
</p:autoComplete>
The converter:
#FacesConverter(forClass=Player.class, value = "PlayerConverter")
public class PlayerConverter implements Converter
{
#Override
public Object getAsObject(FacesContext ctx, UIComponent cmp, String value)
{
Player player = null;
if ( !(value==null || value.trim().isEmpty()))
{
// Autocomplete did find a match, the appropriate
// player is returned
try
{
Integer id = Integer.valueOf(value);
player = TeamService.getInstance().getPlayer(id);
}
catch (NumberFormatException e)
{
// Autocomplete found no match, the entered String
// is given. Create an ad-hoc player as response
player = new Player();
player.setName(value);
player.setAdHoc(true);
}
}
return player;
}
#Override
public String getAsString(FacesContext ctx, UIComponent cmp, Object value)
{
String result = null;
if (value instanceof String)
{
result = (String) value;
}
else if (value instanceof Spieler)
{
Integer id = ((Spieler)value).getId();
result = (id == null) ? null : id.toString();
}
return result;
}
}
The problem I am facing seems simple: how do I coerce this construction into allowing the user to simply erase a value? When the user deletes the content of the input field, I would expect to get a call to PlayerConverter with a null or empty value, but that doesn't happen.
How do I get my hands on empty input?
I found a workaround. Use a converter and add a flag to the objects you are displaying in the p:autocomplete. Set the flag to 'false' in the converter's getAsString method. Set it to 'true' in the converter's getAsObject method. The latter is not called when the user emptied the autocomplete field. Therefore the flag remains 'false'. And that you can check in the action method of your command button.
I hope it helps someone.
In ActionScript 3, is there a clean way to define a function that accepts an optional boolean argument ? As you may know, this is invalid :
public function test(param:Boolean = null):void {
trace(param);
}
This triggers the following error: VerifyError: Error #1102: Illegal default value for type Boolean. Since, Boolean is a primitive, I guess it makes sense that it cannot be set to null. The only workaround I found is to cast the parameter to an object :
public function test(param:Object = null):void {
trace(Boolean(param));
}
However, this does not feel very clean, particularly if you are developing libraries. ASDoc will generate API documentation that says the expected parameter is an Object whereas what is really needed is a Boolean.
Is there a better approach ?
When you say optional, I assume that you mean if there isn't a value supplied then something different should happen compared to if you had a default value of true or false.
You could make your own object to handle the three states that you need and maintain code readability by using a class like this:
public class Condition
{
private var _value:* = null;
public function Condition(initial:* = null)
{
value = initial;
}
public function set value(n:*):void
{
if(_value === null || _value === false || _value === true)
{
_value = n;
}
}
public function get value():*{ return _value; }
}
And then your function could be:
function test(param:Condition = null):void
{
if(param && param.value != null)
{
trace(param.value);
}
}
test( new Condition() );
test( new Condition(true) );
As you said Boolean can not be set to null value.
Therefore, you should specify a default value that is either true or false.
public function test(param:Boolean = false):void {
trace(param);
}
But because you need the third case where nothing is set, one option could be to accept any Object but throw an exception if it is not null and not a boolean:
public function test(param:* = null):void
{
if (param != null)
{
if ((param == true) || (param == false))
{
trace(Boolean(param).toString());
}
else
{
throw new CustomError("param should be a boolean");
}
}
else
{
// Do nothing
}
}
Note that this solution also accept objects or primitives that can be compared to true or false such as 0, 1, or [].
From the good suggestions and discussion above I think that, in a library scenario and for simplicity's sake, the best way remains to type the parameter as Object with a default value of null but to request a Boolean in the API documentation :
/**
* #param param Boolean object or null
*/
public function test(param:Object = null):void {
trace(Boolean(param));
}
This allow the user of the library to pass a either a Boolean or nothing at all. Thanks everyone.
There was a tonne of discussion on my previous answer, but this is the correct way to have a function that accepts one of three states. My previous answer attempted to retain the use of a Boolean value like you were requesting, but that is not the right way to go about it.
Create a class that defines three values:
class State
{
public static const EMPTY:int = -1;
public static const FALSE:int = 0;
public static const TRUE:int = 1;
}
Your function will accept an int (the type of each of the three properties within your State class). It will deal with the three possible values. You can use concise commenting to notify the developer of what thee values the function is expecting, referencing the State class. The default value can be -1 aka State.EMPTY.
/**
* Function description.
* #param state One of three states, defined by State.
*/
function test(state:int = -1):void
{
switch(state)
{
case State.EMPTY:
// No value given.
break;
case State.TRUE:
// True.
//
break;
case State.FALSE:
// False.
//
break;
default:
throw new ArgumentError("Unsupported value for test()");
break;
}
}
A predicate (an object that is a boolean-valued function which tests its input for a condition) is generally assumed to be stateless.
What's the most appropriate name for an object which has a testing function with state?
e.g. in Java, the CountTrigger class below returns true only on the Nth time it is tested against a value that matches a desired value, and false otherwise.
interface QuasiPredicate<T> // what should this be renamed to?
{
public boolean test(T value);
}
class CountTrigger<T> implements QuasiPredicate<T>
{
// for simplicity, ignore synchronization + null-value issues
private int remainingTriggers = 0;
final private T testValue;
public CountTrigger(T testValue, int count)
{
this.remainingTriggers = count;
this.testValue = testValue;
}
#Override public boolean test(T value)
{
if (!this.testValue.equals(value))
return false;
if (this.remainingTriggers == 0)
return false;
if (--this.remainingTriggers == 0)
return true;
}
}
Considering it's an interface and interfaces are implemented and not extended then I don't see the problem in your object implementing a predicate.
If you're going to put public CountTrigger(T testValue, int count) in the interface as well then maybe you need a different name. Perhaps IFiniteRule or another suitable synonym. Maybe ask at https://english.stackexchange.com/ ;-)
Lots of examples are out there for how to map in JAXB using XmlTypeAdapters, and that's great.
However, my situation is I have a field which can by any type (ie: Object.class in Java). What I want is to use an adapter that basically passes all control for marshalling/unmarshalling for that field to me. Again, I don't know the incoming type, nor do I know the outgoing type except at runtime.
For example:
#XmlRootElement( name="myType" )
#XmlAccessorType(XmlAccessType.FIELD)
class MyType {
#XmlElement("value")
#XmlJavaTypeAdapter(ValueAdapter.class)
private Object value;
public static class ValueAdapter extends XmlAdapter<Object, Object> {
public ValueAdapter() {
}
public Object marshal(Object value) {
if (value instanceof Date) {
// do date stuff
}
else if (value instanceof Foo) {
// do foo stuff
}
// ... etc ...
else {
return value;
}
}
public Object unmarshal(Object value) {
if (value instanceof Date) {
// do date stuff
}
else if (value instanceof Foo) {
// do foo stuff
}
// ... etc ...
else {
return value;
}
}
}
// getter/setter fluff ...
}
However, using Jackson, ValueAdapter isn't called.
Is this the right way to do this in JAXB? If not, what is the way to do this?
Glad that it works. Now, since you are using Jackson, there may also be fully automated way.
Specifically for polymorphic types you can often get proper handling by using #JsonTypeInfo annotation -- it will add type information when serializing, use that when deserializing. And as long as actual concrete types have handlers (standard JDK types, beans, collections of other handled types), that's all you need.
I have several data manipulation widgets that all implement a custom IPropertyEditor interface. I would like to include a GetValue getter, but the return types could be String, Boolean or int, off the top of my head. In AS3, all of that inherits from Object, so I could have GetValue return an object, but I don't feel great about that approach. At the risk of asking a subjective question, does anyone have any recommendations on how to approach this?
In ActionScript I'm pretty sure you can set a variable return type by defining a function in the following way:
public function getValue():* {
return "Any of these would be fine.";
return true;
return 1;
return new Sprite();
}
Hope that helps.
In practice, there is an actual getter/setter model in ActionScript. For your case, you could use it like this:
private var _value:*;
public function get value() : * {
return _value;
}
public function set value(val:*) : void {
if (typeof val == "int" || typeof val == "boolean" || typeof val == "string") {
_value = val;
}
}
This limits the user to setting (per your requirements) the value of this "value" property to data types int, Boolean, or String.