#12890 closed enhancement (fixed)
Use Java 8 Predicates
Reported by: | michael2402 | Owned by: | Don-vip |
---|---|---|---|
Priority: | normal | Milestone: | 16.07 |
Component: | Core | Version: | |
Keywords: | gsoc-core predicates java8 | Cc: | Don-vip, bastiK, stoecker |
Description
To make full use of Java 8, we often need predicates.
I suggest making org.openstreetmap.josm.tools.Predicate<T> extend java.util.function.Predicate<T>
We then delegate test to evaluate.
All utility methods return josm predicates but take normal java predicates as arguments. That way, we can switch to java predicates while maintaining compatibility.
Attachments (0)
Change History (18)
comment:1 by , 9 years ago
Milestone: | → 16.06 |
---|
comment:2 by , 9 years ago
follow-up: 4 comment:3 by , 9 years ago
We can use josm predicates for the constants, so they can be used both as java and as josm predicate.
I don't think that we should keep modifiedPredicate
and others with Java8. I don't see any advantage of them. Compare it yourself:
list.filter(OsmPrimitive.modifiedPredicate); list.filter(n -> n.isModified());
Speed would be approx. the same in both cases, the second one has lower memory footprint (since GC can collect the predicate fast)
Read/Write fields should have getters/setters any way.
We can add a wrapper to convert a Predicate to a josm Predicate, that way we can use:
...josm...Predicate getXXXPredicate() setXXXPredicate(...java...Predicate)
follow-up: 5 comment:4 by , 9 years ago
Replying to michael2402:
We can use josm predicates for the constants, so they can be used both as java and as josm predicate.
I don't think that we should keep
modifiedPredicate
and others with Java8. I don't see any advantage of them. Compare it yourself:
list.filter(OsmPrimitive.modifiedPredicate); list.filter(n -> n.isModified());Speed would be approx. the same in both cases, the second one has lower memory footprint (since GC can collect the predicate fast)
Neat, but others (e.g. multipolygonPredicate
) we probably want to keep. The point of making org.openstreetmap.josm.tools.Predicate
extend java.util.function.Predicate
is to have a transition period, where plugins can be fixed. When plugins are ready, we get rid of org.openstreetmap.josm.tools.Predicate
completely.
At that point you have to change the type of multipolygonPredicate
, which breaks any plugins that still use the field.
- If plugins are broken at some point, we might as well do the complete switch at once and be done with it.
- A smooth transition as you planned would be better. I guess, one solution is to duplicate all constants. Plugins are fixed by switching to the new set. The old set of constants is then unused and can be removed. Disadvantage: requires name change or move to new class.
comment:5 by , 9 years ago
Replying to bastiK:
Neat, but others (e.g.
multipolygonPredicate
) we probably want to keep
By adding a new isMultipolygon
method, multipolygonPredicate can be expressed in a nicer way:
p -> p.isMultipolygon()
We do not even need the class comparison and the cast to Relation
, we can use normal inheritance there by simply adding the isMultipolygon method to all primitives and make it return false
except for relations.
Replying to bastiK:
- A smooth transition as you planned would be better. I guess, one solution is to duplicate all constants. Plugins are fixed by switching to the new set. The old set of constants is then unused and can be removed. Disadvantage: requires name change or move to new class.
We can use this to fix the constant names ;-).
But we do not need to change any names or have any dupplicate values.
Old plugin code:
...josm...Predicate<T> pred = SOME_CONSTANT; ...josm...Predicate<T> pred2 = Predicates.not(SOME_CONSTANT);
This can simply be changed to:
...java...Predicate<T> pred = SOME_CONSTANT; ...java...Predicate<T> pred2 = Predicates.not(SOME_CONSTANT);
SOME_CONSTANT is a ...josm...Predicate<T>
which extends ...java...Predicate<T>
. Predicates.not has this signature: ...josm...Predicate<T> not(...java...Predicate<T>)
The plugin does not break when enabling Java predicates and we can use them in mixed mode for some time.
comment:6 by , 9 years ago
As far as I know, this still breaks plugins when the type of the field is changed. It doesn't matter what it is assigned to, just access is enough to raise NoSuchFieldError
.
Anyway, one way or another, this is going to work. Changing to lambda expressions should remove most references to the constant fields.
Idea: Have 2 versions of Main.map.mapView.getNearestNode
(and similar functions), one with java Predicate and one with josm Predicate as argument type. Deprecate the JOSM version -> Plugin developers will get a fixable deprecation warning.
comment:7 by , 9 years ago
Milestone: | 16.06 → 16.07 |
---|
comment:8 by , 9 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
What about fields like
OsmPrimitive.modifiedPredicate
?