Provide a Converter for data-binding by defining a pair of SerializableFunction objects - data-binding

In Vaadin 8 Framework, and Vaadin 10 Flow, the data-binding capability lets us provide a Converter to mediate between the widget’s expected data type (such as String for a TextField) and the data type of the backing bean property (such as Integer number).
In this example, the built-in Converter implementation StringToIntegerConverter is used.
binder
.forField( this.phaseField )
.withConverter(
new StringToIntegerConverter( "Must enter an integer number" )
)
.bind( Panel::getPhase , Panel::setPhase ) ;
But what about defining a Converter for other types? How can I easily define a short-and-sweet Converter? For example, a String-to-UUID converter. I want to show the canonical 36-character hex string in a TextField, and going the other direction, parse that string back into a UUID.
// String to UUID
UUID uuid = UUID.fromString( myString ) ;
// UUID to String
String myString = uuid.toString() ;
I see that Binder.BindingBuilder offers the pair of methods withConverter that both take a pair of SerializableFunction objects.
Binder.BindingBuilder::withConverter(SerializableFunction<TARGET,NEWTARGET> toModel, SerializableFunction<NEWTARGET,TARGET> toPresentation)
Binder.BindingBuilder::withConverter(SerializableFunction<TARGET,NEWTARGET> toModel, SerializableFunction<NEWTARGET,TARGET> toPresentation, String errorMessage)
➥ So how do I define the pair of SerializableFunction objects/classes?
I noticed that this interface lists a known subinterface ValueProvider<SOURCE,TARGET>. That looks familiar, and I have a hunch it is the key to easily defining a short simple converter. But I do not quite comprehend the syntax with lambdas and all that is going on here.
I am not asking how to write a class implementing Converter. I am asking how to write the pair of SerializableFunction arguments to pass to the Binder.BindingBuilder::withConverter methods listed above as bullet items.
Quoting that JavaDoc:
Interface Binder.BindingBuilder<BEAN,TARGET>
…
withConverter
default <NEWTARGET> Binder.BindingBuilder<BEAN,NEWTARGET> withConverter(SerializableFunction<TARGET,NEWTARGET> toModel, SerializableFunction<NEWTARGET,TARGET> toPresentation)
Maps the binding to another data type using the mapping functions and a possible exception as the error message.
The mapping functions are used to convert between a presentation type, which must match the current target data type of the binding, and a model type, which can be any data type and becomes the new target type of the binding. When invoking bind(ValueProvider, Setter), the target type of the binding must match the getter/setter types.
For instance, a TextField can be bound to an integer-typed property using appropriate functions such as: withConverter(Integer::valueOf, String::valueOf);
Type Parameters:
NEWTARGET - the type to convert to
Parameters:
toModel - the function which can convert from the old target type to the new target type
toPresentation - the function which can convert from the new target type to the old target type
Returns:
a new binding with the appropriate type
Throws:
IllegalStateException - if bind has already been called

You can do it by passing two lambda expressions to withConverter, so something like this:
binder.forField(textField)
.withConverter(text -> UUID.fromString(text), uuid -> uuid.toString())
.bind(/* ... */);
If you need a more complicated conversion, then the right-hand side of the lambda can be surrounded with brackets, e.g.
binder.forField(textField).withConverter( text -> {
if ( text == null ) {
return something;
} else {
return somethingElse;
}
}, uuid -> { return uuid.toString(); } )
.bind(/* ... */);

If you need your converter multiple times, I recommend creating a separate class implementing interface com.vaadin.data.Converter. However, using lambdas is possible, too, as you already know (see answer of #ollitietavainen). But this is not Vaadin specific, it's a Java 8+ feature you can read about e.g. here. Basically, you can use lambdas whereever an object implementing an interface with only one method is required.

Related

Dart - Casting List<SuperType> to List<SubType> using generics

I am new to Flutter and Dart, coming from native Android.
Android has a very nice database abstraction architecture called the Room Persistence Library. As far as I am aware, no such database abstraction architecture exists for Flutter using the MVVM / MVC design patterns.
My solution was to create a Dart version of it myself. I got it pretty much done after a few headaches, but I cannot seem to get LiveData to work properly using generics.
I set up my class like this:
class LiveData<T> {
...
}
Now when I want to return some data, it can either be an Object or List<Object>. I found a neat hack for differentiating the two from T:
...
// Parse response
// This checks if the type is an instance of a single entity or a list.
if (entity is T) {
cachedData = rawData.isEmpty ? null : entity.fromMap(rawData.first) as T;
} else {
cachedData = rawData.map((e) => entity.fromMap(e)).toList() as T;
}
...
The problem lies in the second block:
cachedData = rawData.map((e) => entity.fromMap(e)).toList() as T;
With the error:
- Unhandled Exception: type 'List<Entity>' is not a subtype of type 'List<Vehicle>' in type cast
The question then becomes: How can I cast Entity to Vehicle when I do not have access to the Vehicle class. Only an instance of it is assigned to an Entity entity variable.
Here's a snippet to demonstrate my access to Vehicle:
final Entity entity;
...assign Vehicle instance to entity...
print(entity is Vehicle) // True
I've tried using .runtimeType to no avail. I have also thought about splitting LiveData into two classes, the second one being LiveDataList. Although this seems to be the easiest solution to not bug the code- it would bug me (bad pun is intentional) and break the otherwise pretty direct port of Room.
As a temporary solution, I have abstracted out the build logic into a generic function to be passed to the LiveData in the constructor.
final T Function(List<Map<String, dynamic>> rawData) builder;
And now I call that instead of the previous code to build the cachedData.
// Parse response
cachedData = builder(rawData);
With the constructor for the LiveData<List<Vehicle>> called when accessing all vehicles in the Dao<Vehicle> being:
class VehicleDao implements Dao<Vehicle> {
...
static LiveData<List<Vehicle>> get() {
return LiveData<List<Vehicle>>(
...
(rawData) => rawData.map((e) => Vehicle.fromMap(e)).toList(),
...
);
}
}
In Dart (and indeed in many languages) generics screws with the concept of inheritance. You would think that if Bar inherits from Foo, that List<Bar> would also be castable to List<Foo>.
This is not actually going to be the case because of how generics work. When you have a generic class, every time you use that class with a different type, that type is treated as a completely separate class. This is because when the compiler compiles those types, class MyGenericType<Foo> extends BaseClass and class MyGenericType<Bar> extends BaseClass are basically converted to something like class MyGenericType_Foo extends BaseClass and class MyGenericType_Bar extends BaseClass.
Do you see the problem? MyGenericType_Foo and MyGenericType_Bar are not descendants of one another. They are siblings of each other, both extending from BaseClass. This is why when you try to convert a List<Entity> to List<Vehicle>, the cast doesn't work because they are sibling types, not a supertype and subtype.
With all this being said, while you cannot directly cast one generic type to another based on the relationship of the generic type parameter, in the case of List there is a way to convert one List type to another: the cast method.
List<Entity> entityList = <Entity>[...];
List<Vehicle> vehicleList = entityList.cast<Vehicle>(); // This cast will work
One thing to note though, if you are casting from a supertype generic to a sub-type generic and not all the elements of the list are that new type, this cast will throw an error.

JAVAFX architecture for storing user data

I am very new to JAVAFX. I just started looking at how to store user files and in my case I don't want to use XML. I am creating a new version of a tool that in the past was done in perl. The user files were text based, and were done with a proprietary definition.
As an example
PROJECT_NAME some_device;
DATA_BUS_WIDTH 32;
LINE_TYPE_A name_A mask:0xFFFFFFFF default:0x00000000 "Some documentation about this line type
SUB_LINE_TYPE_A_0 sub_name_0 PROP0 "Some documentation about what the sub line does";
SUB_LINE_TYPE_A_1 sub_name_1 PROP0 "Some documentation about what the sub line does";
LINE_TYPE_B name_B PROP_2 Size:0x1000 "Some documentation about this line type - important Linetype B has different properties than the previous line type A"
SUB_LINE_TYPE_B_0 sub_name_0 "Some documentation about what the sub line does";
LINE_TYPE_C name_C Other PROPs "And more documentation"
What I am thinking about doing is creating a document class, and then creating an array, that would hold each of the lines. But the rub is that the document class would hold an array of objects where there are three (or even more) types of objects. A LINE_TYPE_A, LINE_TYPE_B, etc.. objects, where there are different properties for each type. I am familiar with creating an array of One type of object. Creating an array of multiple types, seems odd to me, but it seems like there should be a way. As I rolled through the list, I would have to be able to look at each item and say, you're a TYPE A or your a TYPE C, so I could work with the data appropriately.
Is this the right thing to do to create a custom document format? Or is there something else I should be doing? Again, though, I do want to stay away from XML.
There are a few ways you can go about structuring this:
A) Define a data structure, say DataLine, to hold your data. It would contain all the information in a particular line: TYPE, name, etc. Then continue with what you wanted to do:
class Document {
//array or list or some other collection type of `DataLine`
DataLine[] lines;
void doStuff() {
for (DataLine line : lines) {
// line.getType(), line.getName(), etc
}
}
}
B) Define an inheritance based structure that would isolate common fields / query methods, e.g.
// abstract class if you have some common methods or interface
abstract class DataLine {
abstract DataType getType();
}
// some specific data that belongs to only TypeA
class DataLineTypeA extends / implements DataLine {
}
class Document {
//array or list or some other collection type of `DataLine`
DataLine[] lines;
void doStuff() {
for (DataLine line : lines) {
// can also check with getType() if you have it stored
if (line instanceof DataLineTypeA) {
DataLineTypeA typeA = (DataLineTypeA) line;
// do stuff with typeA specific methods
}
// etc.
}
}
}
Finally, you either create your own data parser if you have a formal definition of your data, or use an intermediate format like JSON. Alternatively, you can make data persistent by using the default Java serialization mechanism.

Fluent binding and conversions

When following the Tutorial for TipCalc's iOS UI, I noticed that the binding method described is obsolete(?) and decided to start using Fluent bindings like described here.
Everything gone fine except for one thing: the iOS slider on the tutorial uses a float value between 0 and 1, and the view model uses a int between 0 and 100. So, obviously, I need a conversion here.
Since it's a two-way-binding, how can bind it to be converted for ViewModel -> View and View -> ViewModel? (ideally with fluent binding)
Also, I'd like to know how can I register a conversion under a "name" to later reuse it. Like it seems to be done on this line.
I tried to search on MvvmCross repos for these named conversions but I didn't find anything like a list of the available conversions, there is such a thing?
Thanks a lot for any help!!
I noticed that the binding method described is obsolete(?)
The message attached to that method is:
[Obsolete("Please use SourceDescribed or FullyDescribed instead")]
So use SourceDescribed if you only want to describe the source, or FullyDescribed if you want to describe the source and target.
set.Bind(label).For(l => l.Text).SourceDescribed("'Hello ' + SourceText");
or:
set.Bind(label).FullyDescribed("Text 'Hello ' + SourceText");
Since it's a two-way-binding, how can bind it to be converted for ViewModel -> View and View -> ViewModel?
Two way converters implement both Convert and ConvertBack.
For example - see:
public class TwoWayConverter : MvxValueConverter<double, string>
{
protected override string Convert(double value, Type targetType, object parameter, CultureInfo culture)
{
return (value*value).ToString();
}
protected override double ConvertBack(string value, Type targetType, object parameter, CultureInfo culture)
{
double doubleValue;
double.TryParse(value, out doubleValue);
return Math.Sqrt(doubleValue);
}
}
from https://github.com/MvvmCross/MvvmCross-Tutorials/blob/master/ValueConversion/ValueConversion.Core/Converters/Converters.cs
I'd like to know how can I register a conversion under a "name" to later reuse it
The names are registered by reflection and convention - see a full description in https://github.com/MvvmCross/MvvmCross/wiki/Value-Converters#referencing-value-converters-in-touch-and-droid
a list of the available conversions
MvvmCross doesn't provide many by default - these are mainly app things.
The only built-in converters that I know of are for:
color
visible
language
command parameter
All of these are discussed in https://github.com/MvvmCross/MvvmCross/wiki/Value-Converters#the-mvx-visibility-valueconverters

Can I cast a string object passed on command line argument to the actual object?

Is it possible to cast a command-line passed string object back to actual object?
I want to do the following, but throwing error can't cast.
Button objPro = (Button) sender;
cProduct cp = (cProduct) objPro.CommandArgument;
If no, then why?
This is what the string holds.
cProduct cpObj = (cProduct)e.Row.DataItem;
Button btnAddProduct = (Button)e.Row.FindControl("btnAddProduct");
if (btnAddProduct != null)
{
btnAddProduct.CommandArgument = cpObj.ToString();
}
You probably can't, because it's a string. It's not a cProduct (whatever that is - consider following .NET naming conventions and naming it Product instead).
Now you could do this if you had a explicit conversion operator in cProduct to create an instance from a string.
You haven't really explained what's in the string, or what's in the type - but if your cProduct type provides a ToString method which contains all the data in a reversible form, then you could easily write a method or a constructor to create the product again:
Product product = new Product(objPro.CommandArgument);
or maybe:
Product product = Product.Parse(objPro.CommandArgument);
You'll have to write that constructor/method, of course.
I would strongly recommend using a constructor or method instead of an operator, just to keep your code clearer - it's very rarely a good idea to write your own conversion operators.
Take a look at CommandArgument on MSDN. The property is a string, when you assign the a value to the property, you aren't casting some complex type to string, you are setting a string value on the property. Can you cast a string back to your object type anyway, regardless of it being a CommandArgument. I doubt it. If the argument is an int you could try int.Parse or similar for other types which have a parse method.

How to set an empty Object's properties programatically?

I'm doing some Actionscript work right now and I'd like to know whether there's a way to initiate an empty object's value programatically like this:
var myObj:Object = new Object;
myObj.add("aKey","aValue");
To add a property called aKey whose value is aValue
I need to create a "Dumb" (data-only) object to use as a parameter to send via POST. So I don't know offhand how long and/or how many attributes it's gonna have.
Or something like that.
Thanks
ActionScript 3 allows you to create new Objects using an expressive Object Literal syntax similar to the one found in JavaScript:
const myObj : Object = {
aKey: "aValue",
};
trace(myObj.aKey); // "aValue"
If you want to assign properties after the object has been constructed then you can use either dot notation or square bracket notation, eg:
const myObj : Object = {}; // create an empty object.
myObj.aKey = "aValue";
myObj["anotherKey"] = "anotherValue";
If you plan on sending the data over HTTP, you may wish to consider looking at the URLVariables class which will take care of URL encoding the data for you.

Resources