yogendra-aurospaces / google-collections

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

InvertibleFunction<A, B> (aka Converter<A, B>) #177

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
In several projects I have added the following interface.  I think it would
go very well in Google Collections:

public interface InvertableFunction<T,K> extends Function<T,K> {
    InvertableFunction<K,T> reverse();
}

With an example implementation of:

public class BasicEncryptor implements InvertableFunction<String,String>{
    private final BasicTextEncryptor encryptor;

    public BasicEncryptor(String encryptionKey) {
        this.encryptor = new BasicTextEncryptor();
        encryptor.setPassword(encryptionKey);
    }

    @Override
    public String apply(String rawString) {
        return encryptor.encrypt(rawString);
    }

    @Override
    public InvertableFunction<String, String> invert() {
        return new InvertableFunction<String, String>() {

            @Override
            public InvertableFunction<String, String> reverse() {
                return BasicEncryptor.this;
            }

            @Override
            public String apply(String from) {
                return encryptor.decrypt(from);
            }
        };
    }
}

Original issue reported on code.google.com by ejwin...@gmail.com on 29 May 2009 at 6:48

GoogleCodeExporter commented 9 years ago
I can see this happening eventually.  It's a little problematic because so many
functions that have an inverse() function are not actually invertible in the
mathmetical sense (for example, you can convert "1.00" to a double and get 1.0, 
then
back to a String and get 1.0), and as a result, the kinds of things you expect 
to be
able to do with an invertible function (like Sets.transform(Set)) aren't really 
ironclad.

I think we can work out how to warn about the minor risks though.

Original comment by kevin...@gmail.com on 29 May 2009 at 6:54

GoogleCodeExporter commented 9 years ago
I just realized that we have actually implemented this already internally, but 
we 
have named it "Converter."

How would users feel if our InvertibleFunction class were named Converter 
instead?

Also, it's an abstract class instead of an interface so that it can have useful 
methods on it like convertAll() (aka transform()).  That's inconsistent with 
our 
other types, but it is quite nice...

Original comment by kevin...@gmail.com on 17 Sep 2009 at 5:33

GoogleCodeExporter commented 9 years ago

Original comment by kevin...@gmail.com on 17 Sep 2009 at 5:34

GoogleCodeExporter commented 9 years ago

Original comment by kevin...@gmail.com on 17 Sep 2009 at 5:45

GoogleCodeExporter commented 9 years ago

Original comment by kevin...@gmail.com on 17 Sep 2009 at 5:57

GoogleCodeExporter commented 9 years ago
Could Converter<T,K> be made to implement InvertibleFunction<T,K>.  Then you can
implement an interface as an option.  The naming would be fine.
Thanks,
Eric

Original comment by ejwin...@gmail.com on 17 Sep 2009 at 6:16

GoogleCodeExporter commented 9 years ago
Definitely make Converter implements InvertibleFunction.

IdentityFunction is one implementation :).

Original comment by stephen....@gmail.com on 3 Dec 2009 at 1:51

GoogleCodeExporter commented 9 years ago
This issue has been moved to the Guava project (keeping the same id number). 
Simply replace 'google-collections' with 'guava-libraries' in your address 
bar and it should take you there.

Original comment by kevinb@google.com on 5 Jan 2010 at 11:09