001    package org.cumulus4j.store.reflectionwrapper.gae;
002    
003    import org.cumulus4j.store.reflectionwrapper.ReflectionWrapper;
004    
005    public class KeyFactory extends ReflectionWrapper {
006    
007            private static KeyFactory instance;
008    
009            protected KeyFactory(ClassLoader classLoader) {
010                    super(classLoader);
011            }
012    
013            public static KeyFactory getInstance() {
014                    if (instance == null)
015                            instance = new KeyFactory(KeyFactory.class.getClassLoader());
016    
017                    return instance;
018            }
019    
020            @Override
021            protected String getWrappedClassName() {
022                    return "com.google.appengine.api.datastore.KeyFactory";
023            }
024    
025            @Override
026            protected Object createWrappedObject(Class<?> wrappedClass) {
027                    throw new UnsupportedOperationException(String.format("The class %s cannot be instantiated! It is solely used in a static way.", getWrappedClassName()));
028            }
029    
030            public Key stringToKey(String encoded) {
031                    Object wrappedKey = invokeStatic(
032                                    1, "stringToKey",
033                                    String.class,
034                                    encoded
035                    );
036    
037                    if (wrappedKey == null)
038                            throw new IllegalStateException("stringToKey(String) returned null! invocationTargetClass=" + getWrappedClassName());
039    
040                    return new Key(this, wrappedKey);
041            }
042    
043            public Key createKey(String kind, long id) {
044                    Object wrappedKey = invokeStatic(
045                                    2, "createKey",
046                                    String.class,
047                                    long.class,
048                                    kind,
049                                    id
050                    );
051    
052                    if (wrappedKey == null)
053                            throw new IllegalStateException("createKey(String, long) returned null! invocationTargetClass=" + getWrappedClassName());
054    
055                    return new Key(this, wrappedKey);
056            }
057    
058            public String createKeyString(String kind, long id) {
059                    String keyString = (String) invokeStatic(
060                                    3, "createKeyString",
061                                    String.class,
062                                    long.class,
063                                    kind,
064                                    id
065                    );
066    
067                    if (keyString == null)
068                            throw new IllegalStateException("createKeyString(String, long) returned null! invocationTargetClass=" + getWrappedClassName());
069    
070                    return keyString;
071            }
072    }