kucci / guava-libraries

Automatically exported from code.google.com/p/guava-libraries
Apache License 2.0
0 stars 0 forks source link

Ordering should have a method for wrapping comparators with function #479

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
I'm missing a method in Ordering which I think should be there;

I tried to stick to Guava coding standards, however it might not be perfect but 
I think it gets the point across:

- In Ordering.java:

public static <I,O> Ordering<O> transformativeOrdering(Function<O,I> function, 
Comparator<I> comparator) {
return new 
TransformativeOrdering(checkNotNull(function),checkNotNull(comparator));
}

@VisibleForTesting class TransformativeOrdering<I, U> extends Ordering<U> {

        private final Function<U, I> transformer;
        private final Comparator<I> internalComparator;

        private TransformativeOrdering(Function<U, I> transformer, Comparator<I> internalComparator) {
            super();
            this.transformer = transformer;
            this.internalComparator = internalComparator;
        }

        @Override
        public int compare(U arg0, U arg1) {
            return internalComparator.compare(transformer.apply(arg0), transformer.apply(arg1));
        }

                @Override
                public String toString() {
                     return "TransformativeOrdering("+internalComparator.toString()+")";
                }

    }

Original issue reported on code.google.com by Bjorn.So...@gmail.com on 18 Nov 2010 at 9:02

GoogleCodeExporter commented 9 years ago
Isn't this the same as Ordering.onResultOf?

http://guava-libraries.googlecode.com/svn/trunk/javadoc/com/google/common/collec
t/Ordering.html#onResultOf(com.google.common.base.Function)

Original comment by daniel.y...@gmail.com on 18 Nov 2010 at 9:12

GoogleCodeExporter commented 9 years ago
You might be right there, I wasn't aware of onResultOf();

Original comment by Bjorn.So...@gmail.com on 18 Nov 2010 at 10:40

GoogleCodeExporter commented 9 years ago
This looks fairly similar to your suggested implementation: 
http://code.google.com/p/guava-libraries/source/browse/trunk/src/com/google/comm
on/collect/ByFunctionOrdering.java  :-)

As @daniel.yokomizo said, you can get this via Ordering.onResultOf().

Original comment by boppenh...@google.com on 20 Nov 2010 at 6:35

GoogleCodeExporter commented 9 years ago
Yup, learn something new every day :)

Original comment by Bjorn.So...@gmail.com on 20 Nov 2010 at 2:18

GoogleCodeExporter commented 9 years ago
This issue has been migrated to GitHub.

It can be found at https://github.com/google/guava/issues/<id>

Original comment by cgdecker@google.com on 1 Nov 2014 at 4:15

GoogleCodeExporter commented 9 years ago

Original comment by cgdecker@google.com on 3 Nov 2014 at 9:09