Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (c) 2002-2013 "Neo Technology," Network Engine for Objects in Lund AB [http://neotechnology.com] This file is part of Neo4j. Neo4j is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.
 
 package org.neo4j.server.helpers;
 
 import java.util.List;
 
 
 /*
  * THIS CLASS SHOULD BE MOVED TO KERNEL ASAP!!!
  */
 public abstract class PropertyTypeDispatcher<K, T>
 {
     public static abstract class PropertyArray<A, T> implements Iterable<T>
     {
         private PropertyArray()
         {
         }
 
         public abstract int length();
 
         public abstract A getClonedArray();
 
         public abstract Class<?> getType();
     }
 
     public static void consumePropertiesPropertyTypeDispatcher<StringVoiddispatcher,
             PropertyContainer entity )
     {
         final Iterable<Stringkeys = entity.getPropertyKeys();
         if (keys instanceof List)
         {
             final List<StringkeysList = (List<String>) keys;
             for (int i = keysList.size() - 1; i >= 0; i--)
             {
                 dispatchProperty(dispatcherentitykeysList.get(i));
             }
         } else
         {
             for (String key : keys)
             {
                 dispatchProperty(dispatcherentitykey);
             }
         }
     }
 
     private static void dispatchProperty(PropertyTypeDispatcher<StringVoiddispatcherPropertyContainer entityString key) {
         Object property = entity.getProperty(keynull);
         if ( property == null ) return;
         dispatcher.dispatchpropertykey );
     }
 
     public static <T> Collection<T> dispatchProperties(
             PropertyTypeDispatcher<String, T> dispatcherPropertyContainer entity )
     {
         List<T> result = new ArrayList<T>();
         for ( String key : entity.getPropertyKeys() )
         {
             Object property = entity.getPropertykeynull );
             if ( property == null ) continue;
             result.adddispatcher.dispatchpropertykey ) );
         }
         return result;
     }
 
     @SuppressWarnings"boxing" )
     public final T dispatchObject property, K param )
     {
         ifproperty == null
         {
             return dispatchNullPropertyparam );
         } else if ( property instanceof String ) 
         {
             return dispatchStringProperty( (Stringpropertyparam );
         }
         else if ( property instanceof Number )
         {
             return dispatchNumberProperty( (Numberpropertyparam );
        }
        else if ( property instanceof Boolean )
        {
            return dispatchBooleanProperty( (Booleanpropertyparam );
        }
        else if ( property instanceof Character )
        {
            return dispatchCharacterProperty( (Characterpropertyparam );
        }
        else if ( property instanceof String[] )
        {
            return dispatchStringArrayProperty( (String[]) propertyparam );
        }
        else if ( property instanceof Object[] )
        {
            return dispatchOtherArray( (Object[]) propertyparam );
        }
        else
        {
            Class<?> propertyType = property.getClass();
            if ( propertyType.isArray() && propertyType.getComponentType().isPrimitive() )
            {
                return dispatchPrimitiveArraypropertyparam );
            }
            else
            {
                return dispatchOtherPropertypropertyparam );
            }
        }
    }
    private T dispatchPrimitiveArrayObject property, K param )
    {
        if ( property instanceof byte[] )
        {
            return dispatchByteArrayProperty( (byte[]) propertyparam );
        }
        else if ( property instanceof char[] )
        {
            return dispatchCharacterArrayProperty( (char[]) propertyparam );
        }
        else if ( property instanceof boolean[] )
        {
            return dispatchBooleanArrayProperty( (boolean[]) propertyparam );
        }
        else if ( property instanceof long[] )
        {
            return dispatchLongArrayProperty( (long[]) propertyparam );
        }
        else if ( property instanceof double[] )
        {
            return dispatchDoubleArrayProperty( (double[]) propertyparam );
        }
        else if ( property instanceof int[] )
        {
            return dispatchIntegerArrayProperty( (int[]) propertyparam );
        }
        else if ( property instanceof short[] )
        {
            return dispatchShortArrayProperty( (short[]) propertyparam );
        }
        else if ( property instanceof float[] )
        {
            return dispatchFloatArrayProperty( (float[]) propertyparam );
        }
        else
        {
            throw new Error"Unsupported primitive array type: " + property.getClass() );
        }
    }
    protected T dispatchOtherArrayObject[] property, K param )
    {
        if ( property instanceof Byte[] )
        {
            return dispatchByteArrayProperty( (Byte[]) propertyparam );
        }
        else if ( property instanceof Character[] )
        {
            return dispatchCharacterArrayProperty( (Character[]) propertyparam );
        }
        else if ( property instanceof Boolean[] )
        {
            return dispatchBooleanArrayProperty( (Boolean[]) propertyparam );
        }
        else if ( property instanceof Long[] )
        {
            return dispatchLongArrayProperty( (Long[]) propertyparam );
        }
        else if ( property instanceof Double[] )
        {
            return dispatchDoubleArrayProperty( (Double[]) propertyparam );
        }
        else if ( property instanceof Integer[] )
        {
            return dispatchIntegerArrayProperty( (Integer[]) propertyparam );
        }
        else if ( property instanceof Short[] )
        {
            return dispatchShortArrayProperty( (Short[]) propertyparam );
        }
        else if ( property instanceof Float[] )
        {
            return dispatchFloatArrayProperty( (Float[]) propertyparam );
        }
        else
        {
            throw new IllegalArgumentException"Unsupported property array type: "
                                                + property.getClass() );
        }
    }
    @SuppressWarnings"boxing" )
    protected T dispatchNumberPropertyNumber property, K param )
    {
        if ( property instanceof Long )
        {
            return dispatchLongProperty( (Longpropertyparam );
        }
        else if ( property instanceof Integer )
        {
            return dispatchIntegerProperty( (Integerpropertyparam );
        }
        else if ( property instanceof Double )
        {
            return dispatchDoubleProperty( (Doublepropertyparam );
        }
        else if ( property instanceof Float )
        {
            return dispatchFloatProperty( (Floatpropertyparam );
        }
        else if ( property instanceof Short )
        {
            return dispatchShortProperty( (Shortpropertyparam );
        }
        else if ( property instanceof Byte )
        {
            return dispatchByteProperty( (Bytepropertyparam );
        }
        else
        {
            throw new IllegalArgumentException"Unsupported property type: " + property.getClass() );
        }
    }
    protected T dispatchNullProperty( K param ) {
        return null;
    }
    
    @SuppressWarnings"boxing" )
    protected abstract T dispatchBytePropertybyte property, K param );
    @SuppressWarnings"boxing" )
    protected abstract T dispatchCharacterPropertychar property, K param );
    @SuppressWarnings"boxing" )
    protected abstract T dispatchShortPropertyshort property, K param );
    @SuppressWarnings"boxing" )
    protected abstract T dispatchIntegerPropertyint property, K param );
    @SuppressWarnings"boxing" )
    protected abstract T dispatchLongPropertylong property, K param );
    @SuppressWarnings"boxing" )
    protected abstract T dispatchFloatPropertyfloat property, K param );
    @SuppressWarnings"boxing" )
    protected abstract T dispatchDoublePropertydouble property, K param );
    @SuppressWarnings"boxing" )
    protected abstract T dispatchBooleanPropertyboolean property, K param );
    
    protected T dispatchOtherPropertyObject property, K param) {
        throw new IllegalArgumentException"Unsupported property array type: "
                + property.getClass() );
    }
    protected T dispatchByteArrayPropertyfinal byte[] property, K param )
    {
        return dispatchByteArrayPropertynew PrimitiveArray<byte[], Byte>()
        {
            @Override
            public byte[] getClonedArray()
            {
                return property.clone();
            }
            @Override
            public int length()
            {
                return property.length;
            }
            @Override
            @SuppressWarnings"boxing" )
            protected Byte itemint offset )
            {
                return property[offset];
            }
            @Override
            public Class<?> getType()
            {
                return property.getClass();
            }
        }, param );
    }
    protected T dispatchCharacterArrayPropertyfinal char[] property, K param )
    {
        return dispatchCharacterArrayPropertynew PrimitiveArray<char[], Character>()
        {
            @Override
            public char[] getClonedArray()
            {
                return property.clone();
            }
            @Override
            public int length()
            {
                return property.length;
            }
            @Override
            @SuppressWarnings"boxing" )
            protected Character itemint offset )
            {
                return property[offset];
            }
            @Override
            public Class<?> getType()
            {
                return property.getClass();
            }
        }, param );
    }
    protected T dispatchShortArrayPropertyfinal short[] property, K param )
    {
        return dispatchShortArrayPropertynew PrimitiveArray<short[], Short>()
        {
            @Override
            public short[] getClonedArray()
            {
                return property.clone();
            }
            @Override
            public int length()
            {
                return property.length;
            }
            @Override
            @SuppressWarnings"boxing" )
            protected Short itemint offset )
            {
                return property[offset];
            }
            @Override
            public Class<?> getType()
            {
                return property.getClass();
            }
        }, param );
    }
    protected T dispatchIntegerArrayPropertyfinal int[] property, K param )
    {
        return dispatchIntegerArrayPropertynew PrimitiveArray<int[], Integer>()
        {
            @Override
            public int[] getClonedArray()
            {
                return property.clone();
            }
            @Override
            public int length()
            {
                return property.length;
            }
            @Override
            @SuppressWarnings"boxing" )
            protected Integer itemint offset )
            {
                return property[offset];
            }
            @Override
            public Class<?> getType()
            {
                return property.getClass();
            }
        }, param );
    }
    protected T dispatchLongArrayPropertyfinal long[] property, K param )
    {
        return dispatchLongArrayPropertynew PrimitiveArray<long[], Long>()
        {
            @Override
            public long[] getClonedArray()
            {
                return property.clone();
            }
            @Override
            public int length()
            {
                return property.length;
            }
            @Override
            @SuppressWarnings"boxing" )
            protected Long itemint offset )
            {
                return property[offset];
            }
            @Override
            public Class<?> getType()
            {
                return property.getClass();
            }
        }, param );
    }
    protected T dispatchFloatArrayPropertyfinal float[] property, K param )
    {
        return dispatchFloatArrayPropertynew PrimitiveArray<float[], Float>()
        {
            @Override
            public float[] getClonedArray()
            {
                return property.clone();
            }
            @Override
            public int length()
            {
                return property.length;
            }
            @Override
            @SuppressWarnings"boxing" )
            protected Float itemint offset )
            {
                return property[offset];
            }
            @Override
            public Class<?> getType()
            {
                return property.getClass();
            }
        }, param );
    }
    protected T dispatchDoubleArrayPropertyfinal double[] property, K param )
    {
        return dispatchDoubleArrayPropertynew PrimitiveArray<double[], Double>()
        {
            @Override
            public double[] getClonedArray()
            {
                return property.clone();
            }
            @Override
            public int length()
            {
                return property.length;
            }
            @Override
            @SuppressWarnings"boxing" )
            protected Double itemint offset )
            {
                return property[offset];
            }
            @Override
            public Class<?> getType()
            {
                return property.getClass();
            }
        }, param );
    }
    protected T dispatchBooleanArrayPropertyfinal boolean[] property, K param )
    {
        return dispatchBooleanArrayPropertynew PrimitiveArray<boolean[], Boolean>()
        {
            @Override
            public boolean[] getClonedArray()
            {
                return property.clone();
            }
            @Override
            public int length()
            {
                return property.length;
            }
            @Override
            @SuppressWarnings"boxing" )
            protected Boolean itemint offset )
            {
                return property[offset];
            }
            @Override
            public Class<?> getType()
            {
                return property.getClass();
            }
        }, param );
    }
    protected T dispatchByteArrayPropertyfinal Byte[] property, K param )
    {
        return dispatchByteArrayPropertynew BoxedArray<byte[], Byte>( property )
        {
            @Override
            @SuppressWarnings"boxing" )
            public byte[] getClonedArray()
            {
                byte[] result = new byte[property.length];
                for ( int i = 0; i < result.lengthi++ )
                    result[i] = property[i];
                return result;
            }
        }, param );
    }
    protected T dispatchCharacterArrayPropertyfinal Character[] property, K param )
    {
        return dispatchCharacterArrayPropertynew BoxedArray<char[], Character>( property )
        {
            @Override
            @SuppressWarnings"boxing" )
            public char[] getClonedArray()
            {
                char[] result = new char[property.length];
                for ( int i = 0; i < result.lengthi++ )
                    result[i] = property[i];
                return result;
            }
        }, param );
    }
    protected T dispatchShortArrayPropertyfinal Short[] property, K param )
    {
        return dispatchShortArrayPropertynew BoxedArray<short[], Short>( property )
        {
            @Override
            @SuppressWarnings"boxing" )
            public short[] getClonedArray()
            {
                short[] result = new short[property.length];
                for ( int i = 0; i < result.lengthi++ )
                    result[i] = property[i];
                return result;
            }
        }, param );
    }
    protected T dispatchIntegerArrayPropertyfinal Integer[] property, K param )
    {
        return dispatchIntegerArrayPropertynew BoxedArray<int[], Integer>( property )
        {
            @Override
            @SuppressWarnings"boxing" )
            public int[] getClonedArray()
            {
                int[] result = new int[property.length];
                for ( int i = 0; i < result.lengthi++ )
                    result[i] = property[i];
                return result;
            }
        }, param );
    }
    protected T dispatchLongArrayPropertyfinal Long[] property, K param )
    {
        return dispatchLongArrayPropertynew BoxedArray<long[], Long>( property )
        {
            @Override
            @SuppressWarnings"boxing" )
            public long[] getClonedArray()
            {
                long[] result = new long[property.length];
                for ( int i = 0; i < result.lengthi++ )
                    result[i] = property[i];
                return result;
            }
        }, param );
    }
    protected T dispatchFloatArrayPropertyfinal Float[] property, K param )
    {
        return dispatchFloatArrayPropertynew BoxedArray<float[], Float>( property )
        {
            @Override
            @SuppressWarnings"boxing" )
            public float[] getClonedArray()
            {
                float[] result = new float[property.length];
                for ( int i = 0; i < result.lengthi++ )
                    result[i] = property[i];
                return result;
            }
        }, param );
    }
    protected T dispatchDoubleArrayPropertyfinal Double[] property, K param )
    {
        return dispatchDoubleArrayPropertynew BoxedArray<double[], Double>( property )
        {
            @Override
            @SuppressWarnings"boxing" )
            public double[] getClonedArray()
            {
                double[] result = new double[property.length];
                for ( int i = 0; i < result.lengthi++ )
                    result[i] = property[i];
                return result;
            }
        }, param );
    }
    protected T dispatchBooleanArrayPropertyfinal Boolean[] property, K param )
    {
        return dispatchBooleanArrayPropertynew BoxedArray<boolean[], Boolean>( property )
        {
            @Override
            @SuppressWarnings"boxing" )
            public boolean[] getClonedArray()
            {
                boolean[] result = new boolean[property.length];
                for ( int i = 0; i < result.lengthi++ )
                    result[i] = property[i];
                return result;
            }
        }, param );
    }
    protected abstract T dispatchStringPropertyString property, K param );
    protected T dispatchStringArrayPropertyfinal String[] property, K param )
    {
        return dispatchStringArrayPropertynew BoxedArray<String[], String>( property )
        {
            @Override
            public String[] getClonedArray()
            {
                return property.clone();
            }
        }, param );
    }
    protected T dispatchStringArrayPropertyPropertyArray<String[], Stringarray, K param )
    {
        return dispatchArrayarrayparam );
    }
    protected T dispatchByteArrayPropertyPropertyArray<byte[], Bytearray, K param )
    {
        return dispatchNumberArrayarrayparam );
    }
    protected T dispatchCharacterArrayPropertyPropertyArray<char[], Characterarray, K param )
    {
        return dispatchArrayarrayparam );
    }
    protected T dispatchShortArrayPropertyPropertyArray<short[], Shortarray, K param )
    {
        return dispatchNumberArrayarrayparam );
    }
    protected T dispatchIntegerArrayPropertyPropertyArray<int[], Integerarray, K param )
    {
        return dispatchNumberArrayarrayparam );
    }
    protected T dispatchLongArrayPropertyPropertyArray<long[], Longarray, K param )
    {
        return dispatchNumberArrayarrayparam );
    }
    protected T dispatchFloatArrayPropertyPropertyArray<float[], Floatarray, K param )
    {
        return dispatchNumberArrayarrayparam );
    }
    protected T dispatchDoubleArrayPropertyPropertyArray<double[], Doublearray, K param )
    {
        return dispatchNumberArrayarrayparam );
    }
    protected T dispatchBooleanArrayPropertyPropertyArray<boolean[], Booleanarray, K param )
    {
        return dispatchArrayarrayparam );
    }
    protected T dispatchNumberArrayPropertyArray<?, ? extends Numberarray, K param )
    {
        return dispatchArrayarrayparam );
    }
    protected T dispatchArrayPropertyArray<?, ?> array, K param )
    {
        throw new UnsupportedOperationException"Unhandled array type: " + array.getType() );
    }
    private static abstract class BoxedArray<A, T> extends PropertyArray<A, T>
    {
        private final T[] array;
        BoxedArray( T[] array )
        {
            this. = array;
        }
        @Override
        public int length()
        {
            return .;
        }
        @Override
        public Iterator<T> iterator()
        {
            return new ArrayIterator<T>(  );
        }
        @Override
        public Class<?> getType()
        {
            return .getClass();
        }
    }
    private static abstract class PrimitiveArray<A, T> extends PropertyArray<A, T>
    {
        @Override
        public Iterator<T> iterator()
        {
            return new Iterator<T>()
            {
                final int size = length();
                int pos = 0;
                @Override
                public boolean hasNext()
                {
                    return  < ;
                }
                @Override
                public T next()
                {
                    return item++ );
                }
                @Override
                public void remove()
                {
                    throw new UnsupportedOperationException(
                            "Cannot remove element from primitive array." );
                }
            };
        }
        protected abstract T itemint offset );
    }
New to GrepCode? Check out our FAQ X