private Object doLookup(String objectExpression, Object defaultValue, boolean searchChildren) {

        if (Str.isEmpty(objectExpression)) {
            return defaultValue;
        }

        char firstChar = Str.idx(objectExpression, 0);
        char secondChar = Str.idx(objectExpression, 1);
        char lastChar = Str.idx(objectExpression, -1);

        boolean escape = false;

        switch(firstChar) {
            case '$':
                if (lastChar=='}') {
                    objectExpression = slc(objectExpression, 2, -1);
                } else {
                    objectExpression = slc(objectExpression, 1);
                }
                break;
            case '{':
                if (secondChar=='{' && lastChar=='}') {
                    char thirdChar = Str.idx(objectExpression, 2);

                    if (thirdChar == '{') {
                        escape = true;
                        objectExpression = slc(objectExpression, 3, -3);
                    }else {
                        objectExpression = slc(objectExpression, 2, -2);
                    }
                }
                else {
                    if (lastChar == '}') {
                        return jsonParser.parse(objectExpression);
                    } else {
                        escape = true;
                        objectExpression = slc(objectExpression, 1);
                    }
                }
                break;
            case '[':
                return jsonParser.parse(objectExpression);
            case '.':
                if (secondChar=='.') {

                    String newExp = slc(objectExpression, 2);
                    return parent.doLookup(newExp, newExp, false);
                }
        }

        Object value;
        lastChar = Str.idx(objectExpression, -1);
        if (lastChar==')') {
            value = handleFunction(objectExpression, searchChildren);
        } else {

            value = findProperty(objectExpression, searchChildren);

            value = value == null ? defaultValue : value;

        }

        if (!escape) {
            return value;
        } else {
            return StandardFunctions.escapeXml(value);
        }

    }

private Object doLookup ( String objectExpression , Object defaultValue , boolean searchChildren ) { if ( Str . isEmpty ( objectExpression ) ) { return defaultValue ; } char firstChar = Str . idx ( objectExpression , 0 ) ; char secondChar = Str . idx ( objectExpression , 1 ) ; char lastChar = Str . idx ( objectExpression , - 1 ) ; boolean escape = false ; switch ( firstChar ) { case ' ' : if ( lastChar == ' ' ) { objectExpression = slc ( objectExpression , 2 , - 1 ) ; } else { objectExpression = slc ( objectExpression , 1 ) ; } break ; case ' ' : if ( secondChar == ' ' && lastChar == ' ' ) { char thirdChar = Str . idx ( objectExpression , 2 ) ; if ( thirdChar == ' ' ) { escape = true ; objectExpression = slc ( objectExpression , 3 , - 3 ) ; } else { objectExpression = slc ( objectExpression , 2 , - 2 ) ; } } else { if ( lastChar == ' ' ) { return jsonParser . parse ( objectExpression ) ; } else { escape = true ; objectExpression = slc ( objectExpression , 1 ) ; } } break ; case ' ' : return jsonParser . parse ( objectExpression ) ; case ' ' : if ( secondChar == ' ' ) { String newExp = slc ( objectExpression , 2 ) ; return parent . doLookup ( newExp , newExp , false ) ; } } Object value ; lastChar = Str . idx ( objectExpression , - 1 ) ; if ( lastChar == ' ' ) { value = handleFunction ( objectExpression , searchChildren ) ; } else { value = findProperty ( objectExpression , searchChildren ) ; value = value == null ? defaultValue : value ; } if ( ! escape ) { return value ; } else { return StandardFunctions . escapeXml ( value ) ; } }

Lookup an object and supply a default value.
@param objectExpression
@param defaultValue
@return

Lookup an object and supply a default value .


@SuppressWarnings( "unchecked" )
    public static <T> T fromMap( Map<String, Object> map, Class<T> clazz ) {
        return mapper.fromMap(map, clazz);
    }

@ SuppressWarnings ( "unchecked" ) public static < T > T fromMap ( Map < String , Object > map , Class < T > clazz ) { return mapper . fromMap ( map , clazz ) ; }

From map.
@param map map to create the object from.
@param clazz the new instance type
@param <T> generic type capture
@return new object


@SuppressWarnings( "unchecked" )
    public static <T> T fromMap( Map<String, Object> map, Class<T> clazz, String... excludeProperties ) {
        Set<String> ignoreProps = excludeProperties.length > 0 ? Sets.set(excludeProperties) :  null;
        return new MapperComplex(FieldAccessMode.FIELD_THEN_PROPERTY.create( false ), ignoreProps, null, true).fromMap(map, clazz);

    }

@ SuppressWarnings ( "unchecked" ) public static < T > T fromMap ( Map < String , Object > map , Class < T > clazz , String ... excludeProperties ) { Set < String > ignoreProps = excludeProperties . length > 0 ? Sets . set ( excludeProperties ) : null ; return new MapperComplex ( FieldAccessMode . FIELD_THEN_PROPERTY . create ( false ) , ignoreProps , null , true ) . fromMap ( map , clazz ) ; }

fromMap converts a map into a java object.
@param map map to create the object from.
@param clazz  the new instance type
@param excludeProperties the properties to exclude
@param <T> generic type capture
@return the new object

fromMap converts a map into a java object .


@SuppressWarnings("unchecked")
    public static <T> T fromValueMap( boolean respectIgnore, String view, final FieldsAccessor fieldsAccessor,
                                      final Map<String, Value> valueMap,
                                      final Class<T> cls, Set<String> ignoreSet ) {

        Mapper mapper = new MapperComplex(fieldsAccessor, ignoreSet, view, respectIgnore);
        return mapper.fromValueMap(valueMap, cls);

     }

@ SuppressWarnings ( "unchecked" ) public static < T > T fromValueMap ( boolean respectIgnore , String view , final FieldsAccessor fieldsAccessor , final Map < String , Value > valueMap , final Class < T > cls , Set < String > ignoreSet ) { Mapper mapper = new MapperComplex ( fieldsAccessor , ignoreSet , view , respectIgnore ) ; return mapper . fromValueMap ( valueMap , cls ) ; }

Creates an object from a value map.

This does some special handling to take advantage of us using the value map so it avoids creating
a bunch of array objects and collections. Things you have to worry about when writing a
high-speed JSON serializer.
@param respectIgnore  honor @JsonIgnore, transients, etc. of the field
@param view the view of the object which can ignore certain fields given certain views
@param fieldsAccessor how we are going to access the fields (by field, by property, combination)
@param cls the new type
@param ignoreSet a set of properties to ignore
@return new object from value map


public static Map<String, Object> toMap( final Object object, final String... ignore ) {
        return toMap( object, Sets.set( ignore ) );
    }

public static Map < String , Object > toMap ( final Object object , final String ... ignore ) { return toMap ( object , Sets . set ( ignore ) ) ; }

Basic toMap to create an object into a map.
@param object the object we want to convert to a list
@param ignore do we honor ignore properties
@return new map

Basic toMap to create an object into a map .


public static Map<String, Object> toMap( final Object object, Set<String> ignore ) {

        return new MapperComplex(ignore).toMap(object);

    }

public static Map < String , Object > toMap ( final Object object , Set < String > ignore ) { return new MapperComplex ( ignore ) . toMap ( object ) ; }

This could be refactored to use core.TypeType class and it would run faster.
Converts an object into a map
@param object the object that we want to convert
@param ignore the map
@return map map representation of the object

This could be refactored to use core . TypeType class and it would run faster . Converts an object into a map


Map<String, Object> doGetValues(Annotation annotation) {
        /* Holds the value map. */
        Map<String, Object> values = new HashMap<String, Object>();
        /* Get the declared staticMethodMap from the actual annotation. */
        Method[] methods = annotation.annotationType().getDeclaredMethods();

        final Object[] noargs = ( Object[] ) null;

        /* Iterate through declared staticMethodMap and extract values
         * by invoking decalared staticMethodMap if they are no arg staticMethodMap.
         */
        for ( Method method : methods ) {
            /* If it is a no arg method assume it is an annoation value. */
            if ( method.getParameterTypes().length == 0 ) {
                try {
                    /* Get the value. */
                    Object value = method.invoke( annotation, noargs );
                    if (value instanceof Enum) {
                        Enum enumVal = (Enum)value;
                        value = enumVal.name ();
                    }
                    values.put( method.getName(), value );
                } catch ( Exception ex ) {
                    throw new RuntimeException( ex );
                }
            }
        }
        return values;
    }

Get the values from the annotation.
We use reflection to turn the annotation into a simple HashMap
of values.

@return

Get the values from the annotation . We use reflection to turn the annotation into a simple HashMap of values .


protected CompositeValidator createValidator(
            List<ValidatorMetaData> validationMetaDataList ) {

        /*
         * A field (property) can be associated with many validators so we use a
         * CompositeValidator to hold all of the validators associated with this
         * validator.
         */
        CompositeValidator compositeValidator = new CompositeValidator(); // hold
        // all
        // of
        // the
        // validators
        // associated
        // with
        // the
        // field.

        /*
         * Lookup the list of validators for the current field and initialize
         * them with validation meta-data properties.
         */
        List<FieldValidator> validatorsList =
                lookupTheListOfValidatorsAndInitializeThemWithMetaDataProperties( validationMetaDataList );

        compositeValidator.setValidatorList( validatorsList );

        return compositeValidator;
    }

Create the validator by looking it up in the ObjectRegistry and then
populating it with values from the meta-data list.

@param validationMetaDataList Holds metadataInformation about validation.
@return composite validator with all of the validators for this property present.

Create the validator by looking it up in the ObjectRegistry and then populating it with values from the meta - data list .


private List<FieldValidator>
    lookupTheListOfValidatorsAndInitializeThemWithMetaDataProperties(
            List<ValidatorMetaData> validationMetaDataList ) {

        List<FieldValidator> validatorsList = new ArrayList<>();

        /*
         * Look up the crank validators and then apply the properties from the
         * validationMetaData to them.
         */
        for ( ValidatorMetaData validationMetaData : validationMetaDataList ) {
            /* Look up the FieldValidator. */
            FieldValidator validator = lookupValidatorInRegistry(
                    validationMetaData
                            .getName() );
            /*
             * Apply the properties from the validationMetaData to the
             * validator.
             */
            applyValidationMetaDataPropertiesToValidator( validationMetaData,
                    validator );
            validatorsList.add( validator );
        }
        return validatorsList;
    }

Lookup the list of validators for the current field and initialize them
with validation meta-data properties.

@param validationMetaDataList list of validation meta-data
@return list of field validators.

Lookup the list of validators for the current field and initialize them with validation meta - data properties .


private FieldValidator lookupValidatorInRegistry(
            String validationMetaDataName ) {

        Map<String, Object> applicationContext = ValidationContext.get().getObjectRegistry();

        Exceptions.requireNonNull( applicationContext );

        return ( FieldValidator ) applicationContext
                .get( "/org/boon/validator/" + validationMetaDataName );
    }

private FieldValidator lookupValidatorInRegistry ( String validationMetaDataName ) { Map < String , Object > applicationContext = ValidationContext . get ( ) . getObjectRegistry ( ) ; Exceptions . requireNonNull ( applicationContext ) ; return ( FieldValidator ) applicationContext . get ( "/org/boon/validator/" + validationMetaDataName ) ; }

This method looks up the validator in the registry.

@param validationMetaDataName The name of the validator that we are looking up.
@return field validator

This method looks up the validator in the registry .


private void applyValidationMetaDataPropertiesToValidator(
            ValidatorMetaData metaData, FieldValidator validator ) {
        Map<String, Object> properties = metaData.getProperties();
        ifPropertyBlankRemove( properties, "detailMessage" );
        ifPropertyBlankRemove( properties, "summaryMessage" );

        BeanUtils.copyProperties( validator,
                properties );
    }

This method applies the properties from the validationMetaData to the
validator uses Spring's BeanWrapperImpl.

@param metaData  validation meta data
@param validator field validator

This method applies the properties from the validationMetaData to the validator uses Spring s BeanWrapperImpl .


private void ifPropertyBlankRemove( Map<String, Object> properties, String property ) {

        Object object = properties.get( property );
        if ( object == null ) {
            properties.remove( property );
        } else if ( object instanceof String ) {
            String string = ( String ) object;
            if ( "".equals( string.trim() ) ) {
                properties.remove( property );
            }
        }
    }

private void ifPropertyBlankRemove ( Map < String , Object > properties , String property ) { Object object = properties . get ( property ) ; if ( object == null ) { properties . remove ( property ) ; } else if ( object instanceof String ) { String string = ( String ) object ; if ( "" . equals ( string . trim ( ) ) ) { properties . remove ( property ) ; } } }

Removes a property if it is null or an empty string.
This allows the property to have a null or emtpy string in the
meta-data but we don't copy it to the validator if the property
is not set.

@param properties properties
@param property   property

Removes a property if it is null or an empty string . This allows the property to have a null or emtpy string in the meta - data but we don t copy it to the validator if the property is not set .


@Override
    public void tick(long time) {
        this.time.set(time);
         /*Foreign thread    every 20 or so mili-seconds so we don't spend too
         much time figuring out utc time. */
        approxTime.set(Dates.utcNow());
    }

Calculate utc time. This gets called every 20 mili-seconds or so.

Calculate utc time . This gets called every 20 mili - seconds or so .


@Override
    public int size() {
        int size = 0;
        for ( SimpleCache<K, V> cache : cacheRegions ) {
            size += cache.size();
        }
        return size;
    }

@ Override public int size ( ) { int size = 0 ; for ( SimpleCache < K , V > cache : cacheRegions ) { size += cache . size ( ) ; } return size ; }

Get the size of the cache.
This is not 100% accurate if cache is being concurrenly accessed.

Get the size of the cache . This is not 100% accurate if cache is being concurrenly accessed .


private final int hash( Object k ) {
        int h = hashSeed;

        h ^= k.hashCode();

        h ^= ( h >>> 20 ) ^ ( h >>> 12 );
        return h ^ ( h >>> 7 ) ^ ( h >>> 4 );
    }

private final int hash ( Object k ) { int h = hashSeed ; h ^= k . hashCode ( ) ; h ^= ( h >>> 20 ) ^ ( h >>> 12 ) ; return h ^ ( h >>> 7 ) ^ ( h >>> 4 ) ; }

Calculate the hash.
@param k key
@return


private Options defaultOptions() {

        Options options = new Options();
        options.createIfMissing(true);
        options.blockSize(32_768); //32K
        options.cacheSize(67_108_864);//64MB
        return options;
    }

private Options defaultOptions ( ) { Options options = new Options ( ) ; options . createIfMissing ( true ) ; options . blockSize ( 32_768 ) ; //32K options . cacheSize ( 67_108_864 ) ; //64MB return options ; }


private boolean openDB(File file, Options options) {

        try {
            database = JniDBFactory.factory.open(file, options);
            logger.info("Using JNI Level DB");
            return true;
        } catch (IOException ex1) {
            try {
                database = Iq80DBFactory.factory.open(file, options);
                logger.info("Using Java Level DB");
                return false;
            } catch (IOException ex2) {
                return Exceptions.handle(Boolean.class, ex2);
            }
        }

    }

private
boolean
openDB
(
File
file
,
Options
options
)
{
try
{
database
=
JniDBFactory
.
factory
.
open
(
file
,
options
)
;
logger
.
info
(
"Using JNI Level DB"
)
;
return
true
;
}
catch
(
IOException
ex1
)
{
try
{
database
=
Iq80DBFactory
.
factory
.
open
(
file
,
options
)
;
logger
.
info
(
"Using Java Level DB"
)
;
return
false
;
}
catch
(
IOException
ex2
)
{
return
Exceptions
.
handle
(
Boolean
.
class
,
ex2
)
;
}
}
}

Opens the database

@param file    filename to open
@param options options
@return


@Override
    public void putAll(Map<byte[], byte[]> values) {

        WriteBatch batch = database.createWriteBatch();

        try {

            for (Map.Entry<byte[], byte[]> entry : values.entrySet()) {
                batch.put(entry.getKey(), entry.getValue());
            }

            if (putAllWriteCount.addAndGet(values.size()) > 10_000) {
                putAllWriteCount.set(0);
                database.write(batch, flush);
            } else {
                database.write(batch, writeOptions);
            }

        } finally {
            closeBatch(batch);
        }
    }

@ Override public void putAll ( Map < byte [ ] , byte [ ] > values ) { WriteBatch batch = database . createWriteBatch ( ) ; try { for ( Map . Entry < byte [ ] , byte [ ] > entry : values . entrySet ( ) ) { batch . put ( entry . getKey ( ) , entry . getValue ( ) ) ; } if ( putAllWriteCount . addAndGet ( values . size ( ) ) > 10_000 ) { putAllWriteCount . set ( 0 ) ; database . write ( batch , flush ) ; } else { database . write ( batch , writeOptions ) ; } } finally { closeBatch ( batch ) ; } }

Puts values into the key value store in batch mode

@param values values


@Override
    public void removeAll(Iterable<byte[]> keys) {

        WriteBatch batch = database.createWriteBatch();

        try {

            for (byte[] key : keys) {
                batch.delete(key);
            }

            database.write(batch);

        } finally {
            closeBatch(batch);
        }

    }

@ Override public void removeAll ( Iterable < byte [ ] > keys ) { WriteBatch batch = database . createWriteBatch ( ) ; try { for ( byte [ ] key : keys ) { batch . delete ( key ) ; } database . write ( batch ) ; } finally { closeBatch ( batch ) ; } }

Remove all of the keys passed.

@param keys keys


@Override
    public KeyValueIterable<byte[], byte[]> search(byte[] startKey) {

        final DBIterator iterator = database.iterator();
        iterator.seek(startKey);

        return new KeyValueIterable<byte[], byte[]>() {
            @Override
            public void close() {
                closeIterator(iterator);
            }

            @Override
            public Iterator<Entry<byte[], byte[]>> iterator() {
                return new Iterator<Entry<byte[], byte[]>>() {
                    @Override
                    public boolean hasNext() {
                        return iterator.hasNext();
                    }

                    @Override
                    public Entry<byte[], byte[]> next() {

                        Map.Entry<byte[], byte[]> next = iterator.next();
                        return new Entry<>(next.getKey(), next.getValue());
                    }

                    @Override
                    public void remove() {
                        iterator.remove();
                    }
                };
            }
        };

    }

@ Override public KeyValueIterable < byte [ ] , byte [ ] > search ( byte [ ] startKey ) { final DBIterator iterator = database . iterator ( ) ; iterator . seek ( startKey ) ; return new KeyValueIterable < byte [ ] , byte [ ] > ( ) { @ Override public void close ( ) { closeIterator ( iterator ) ; } @ Override public Iterator < Entry < byte [ ] , byte [ ] > > iterator ( ) { return new Iterator < Entry < byte [ ] , byte [ ] > > ( ) { @ Override public boolean hasNext ( ) { return iterator . hasNext ( ) ; } @ Override public Entry < byte [ ] , byte [ ] > next ( ) { Map . Entry < byte [ ] , byte [ ] > next = iterator . next ( ) ; return new Entry <> ( next . getKey ( ) , next . getValue ( ) ) ; } @ Override public void remove ( ) { iterator . remove ( ) ; } } ; } } ; }

Search to a certain location.

@param startKey startKey
@return


@Override
    public Map<byte[], byte[]> loadAllByKeys(Collection<byte[]> keys) {

        if (keys == null || keys.size() == 0) {
            return Collections.EMPTY_MAP;
        }

        Map<byte[], byte[]> results = new LinkedHashMap<>(keys.size());

        DBIterator iterator = null;

        try {
            iterator = database.iterator();

            iterator.seek(keys.iterator().next());

            while (iterator.hasNext()) {
                final Map.Entry<byte[], byte[]> next = iterator.next();
                results.put(next.getKey(), next.getValue());
            }
        } finally {
            try {
                if (iterator != null) {
                    iterator.close();
                }
            } catch (IOException e) {
                Exceptions.handle(e);
            }
        }
        return results;

    }

@ Override public Map < byte [ ] , byte [ ] > loadAllByKeys ( Collection < byte [ ] > keys ) { if ( keys == null || keys . size ( ) == 0 ) { return Collections . EMPTY_MAP ; } Map < byte [ ] , byte [ ] > results = new LinkedHashMap <> ( keys . size ( ) ) ; DBIterator iterator = null ; try { iterator = database . iterator ( ) ; iterator . seek ( keys . iterator ( ) . next ( ) ) ; while ( iterator . hasNext ( ) ) { final Map . Entry < byte [ ] , byte [ ] > next = iterator . next ( ) ; results . put ( next . getKey ( ) , next . getValue ( ) ) ; } } finally { try { if ( iterator != null ) { iterator . close ( ) ; } } catch ( IOException e ) { Exceptions . handle ( e ) ; } } return results ; }

Keys are expected to be sorted

@param keys
@return


@Override
    public void close() {
        try {
            flush();
            database.close();
        } catch (Exception e) {
            Exceptions.handle(e);
        }
    }

@ Override public void close ( ) { try { flush ( ) ; database . close ( ) ; } catch ( Exception e ) { Exceptions . handle ( e ) ; } }


public  static <T> double reduceBy( final double[] array, T object ) {
        if (object.getClass().isAnonymousClass()) {
            return reduceByR(array, object );
        }

        try {
            ConstantCallSite callSite = Invoker.invokeReducerLongIntReturnLongMethodHandle(object);
            MethodHandle methodHandle = callSite.dynamicInvoker();
            try {

                double sum = 0;
                for ( double v : array ) {
                    sum = (double) methodHandle.invokeExact( sum, v );

                }
                return sum;
            } catch (Throwable throwable) {
                return handle(Long.class, throwable, "Unable to perform reduceBy");
            }
        } catch (Exception ex) {
            return reduceByR(array, object);
        }

    }

Reduce by functional support for int arrays.
@param array array of items to reduce by
@param object object that contains the reduce by function
@param <T> the type of object
@return the final reduction

Reduce by functional support for int arrays .


private  static <T> double reduceByR( final double[] array, T object ) {
        try {

            Method method = Invoker.invokeReducerLongIntReturnLongMethod(object);

            double sum = 0;
            for ( double v : array ) {
                sum = (double) method.invoke(object, sum, v);

            }
            return sum;

        } catch (Throwable throwable) {
            return handle(Long.class, throwable, "Unable to perform reduceBy");
        }

    }

private
static
<
T
>
double
reduceByR
(
final
double
[
]
array
,
T
object
)
{
try
{
Method
method
=
Invoker
.
invokeReducerLongIntReturnLongMethod
(
object
)
;
double
sum
=
0
;
for
(
double
v
:
array
)
{
sum
=
(
double
)
method
.
invoke
(
object
,
sum
,
v
)
;
}
return
sum
;
}
catch
(
Throwable
throwable
)
{
return
handle
(
Long
.
class
,
throwable
,
"Unable to perform reduceBy"
)
;
}
}

Fallback to reflection if the call-site will not work or did not work
@param array array of items to reduce by
@param object function object
@param <T> type of function object.
@return

Fallback to reflection if the call - site will not work or did not work


public static double varianceDouble(double[] values, final int start, final int length) {
        double mean = mean(values, start, length);
        double temp = 0;
        for(int index = start; index < length; index++) {
            double a = values[index];
            temp += (mean-a)*(mean-a);
        }
        return temp / length;
    }

public static double varianceDouble ( double [ ] values , final int start , final int length ) { double mean = mean ( values , start , length ) ; double temp = 0 ; for ( int index = start ; index < length ; index ++ ) { double a = values [ index ] ; temp += ( mean - a ) * ( mean - a ) ; } return temp / length ; }

Calculate Variance.

@param values values
@param start start
@param length length
@return variance


public static double meanDouble( long[] values, final int start, final int length ) {
        double mean = ((double)sum(values, start, length))/ ((double) length);
        return mean;
    }

public static double meanDouble ( long [ ] values , final int start , final int length ) { double mean = ( ( double ) sum ( values , start , length ) ) / ( ( double ) length ) ; return mean ; }

Used internally to avoid loss and rounding errors a bit.
@param values values
@param start start
@param length length
@return meanDouble

Used internally to avoid loss and rounding errors a bit .


public static Object invokeMethodFromObjectArg(Object object, MethodAccess method, Object args) {
        return invokeMethodFromObjectArg(false, null, null, object, method, args);

    }

public static Object invokeMethodFromObjectArg ( Object object , MethodAccess method , Object args ) { return invokeMethodFromObjectArg ( false , null , null , object , method , args ) ; }

Invokes method from list or map depending on what the Object arg is.
@param object
@param method
@param args
@return

Invokes method from list or map depending on what the Object arg is .


public static double reduceBy( final float[] array, ReduceBy reduceBy ) {

        double sum = 0;
        for ( float v : array ) {
            sum = reduceBy.reduce(sum, v);
        }
        return sum;
    }

public static double reduceBy ( final float [ ] array , ReduceBy reduceBy ) { double sum = 0 ; for ( float v : array ) { sum = reduceBy . reduce ( sum , v ) ; } return sum ; }

A very fast reduce by.
If performance is your thing, this seems to be as fast a plain for loop when benchmarking with JMH.

@param array array of items to reduce by
@param reduceBy reduceBy interface
@return the final value

A very fast reduce by . If performance is your thing this seems to be as fast a plain for loop when benchmarking with JMH .


public static String euroUTCSystemDateString( long timestamp ) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis( timestamp );
        calendar.setTimeZone( UTC_TIME_ZONE );
        int day = calendar.get( Calendar.DAY_OF_MONTH );
        int month = calendar.get( Calendar.MONTH );
        int year = calendar.get( Calendar.YEAR );
        int hour = calendar.get( Calendar.HOUR_OF_DAY );
        int minute = calendar.get( Calendar.MINUTE );
        int second = calendar.get( Calendar.SECOND );

        CharBuf buf = CharBuf.create( 16 );
        buf.add( Str.zfill ( day, 2 ) ).add( '_' );
        buf.add( Str.zfill( month, 2 ) ).add( '_' );
        buf.add( year ).add( '_' );
        buf.add( Str.zfill( hour, 2 ) ).add( '_' );
        buf.add( Str.zfill( minute, 2 ) ).add( '_' );
        buf.add( Str.zfill( second, 2 ) ).add( "_utc_euro" );

        return buf.toString();
    }

public static String euroUTCSystemDateString ( long timestamp ) { Calendar calendar = Calendar . getInstance ( ) ; calendar . setTimeInMillis ( timestamp ) ; calendar . setTimeZone ( UTC_TIME_ZONE ) ; int day = calendar . get ( Calendar . DAY_OF_MONTH ) ; int month = calendar . get ( Calendar . MONTH ) ; int year = calendar . get ( Calendar . YEAR ) ; int hour = calendar . get ( Calendar . HOUR_OF_DAY ) ; int minute = calendar . get ( Calendar . MINUTE ) ; int second = calendar . get ( Calendar . SECOND ) ; CharBuf buf = CharBuf . create ( 16 ) ; buf . add ( Str . zfill ( day , 2 ) ) . add ( ' ' ) ; buf . add ( Str . zfill ( month , 2 ) ) . add ( ' ' ) ; buf . add ( year ) . add ( ' ' ) ; buf . add ( Str . zfill ( hour , 2 ) ) . add ( ' ' ) ; buf . add ( Str . zfill ( minute , 2 ) ) . add ( ' ' ) ; buf . add ( Str . zfill ( second , 2 ) ) . add ( "_utc_euro" ) ; return buf . toString ( ) ; }

Useful for generated file names and generated work directories.

@param timestamp the timestamp
@return euro style format.

Useful for generated file names and generated work directories .


public static boolean matches( Object obj, Criteria... exp ) {
        return ObjectFilter.and( exp ).test( obj );
    }

public static boolean matches ( Object obj , Criteria ... exp ) { return ObjectFilter . and ( exp ) . test ( obj ) ; }

Does the object match this expression.
An expression is a collection of criteria.
@param obj object in question
@param exp expression
@return true or false

Does the object match this expression . An expression is a collection of criteria .


public static Criterion notIn( final Object name, final Object... values ) {
        return new Criterion<Object>( name.toString(), Operator.NOT_IN, values ) {

            @Override
            public boolean resolve( Object owner ) {

                Object fieldValue = fieldValue();
                if ( value == null ) {
                    return false;
                }
                return !valueSet().contains( fieldValue );
            }
        };
    }

public static Criterion notIn ( final Object name , final Object ... values ) { return new Criterion < Object > ( name . toString ( ) , Operator . NOT_IN , values ) { @ Override public boolean resolve ( Object owner ) { Object fieldValue = fieldValue ( ) ; if ( value == null ) { return false ; } return ! valueSet ( ) . contains ( fieldValue ) ; } } ; }

This has to convert values to field type.

This has to convert values to field type .


public static Criteria criteriaFromList(List<?> list) {

        List<Object> args = new ArrayList(list);

        Object o = atIndex(args, -1);
        if (! (o instanceof List) ) {
            atIndex(args, -1, Collections.singletonList(o));
        }

        return (Criteria) Invoker.invokeFromList(ObjectFilter.class,
                "createCriteriaFromClass", args);

    }

public static Criteria criteriaFromList ( List < ? > list ) { List < Object > args = new ArrayList ( list ) ; Object o = atIndex ( args , - 1 ) ; if ( ! ( o instanceof List ) ) { atIndex ( args , - 1 , Collections . singletonList ( o ) ) ; } return ( Criteria ) Invoker . invokeFromList ( ObjectFilter . class , "createCriteriaFromClass" , args ) ; }

Creates criteria from a list. This is used to configure Criteria in JSON.
@param list list to configure a single criteria
@return criteria

Creates criteria from a list . This is used to configure Criteria in JSON .


public static Criteria criteriaFromJson(String json) {

        return (Criteria) Invoker.invokeFromObject(ObjectFilter.class,
                "createCriteriaFromClass", fromJson(json));

    }

public static Criteria criteriaFromJson ( String json ) { return ( Criteria ) Invoker . invokeFromObject ( ObjectFilter . class , "createCriteriaFromClass" , fromJson ( json ) ) ; }

Converts a JSON string into a Criteria.
@param json
@return

Converts a JSON string into a Criteria .


public boolean addArray(double... integers) {
        if (end + integers.length >= values.length) {
            values = grow(values, (values.length + integers.length) * 2);
        }

        System.arraycopy(integers, 0, values, end, integers.length);
        end += integers.length;
        return true;
    }

public boolean addArray ( double ... integers ) { if ( end + integers . length >= values . length ) { values = grow ( values , ( values . length + integers . length ) * 2 ) ; } System . arraycopy ( integers , 0 , values , end , integers . length ) ; end += integers . length ; return true ; }

Add a new array to the list.

@return was able to add.


public static <T> T max( T[] array ) {

        if (array.length > 1) {
            Sorting.sortDesc(array);

            return array[0];
        } else {
            return null;
        }
    }

public static < T > T max ( T [ ] array ) { if ( array . length > 1 ) { Sorting . sortDesc ( array ) ; return array [ 0 ] ; } else { return null ; } }

Gets the max item from the array.
Sorts the list descending first.

@param array the list
@param <T> type of items
@return the max item

Gets the max item from the array . Sorts the list descending first .


public static <T> List<T> firstOf( List<T> list, int count, Sort... sorts ) {

        if (list.size()>1) {
            Sorting.sort(list, sorts);

            return Lists.sliceOf(list, 0, count);
        } else {
            return null;
        }
    }

public static < T > List < T > firstOf ( List < T > list , int count , Sort ... sorts ) { if ( list . size ( ) > 1 ) { Sorting . sort ( list , sorts ) ; return Lists . sliceOf ( list , 0 , count ) ; } else { return null ; } }

From the sorts, this is the first few items.

@param list
@param count
@param sorts
@param <T>
@return

From the sorts this is the first few items .


public static <T> T lastOf( List<T> list, Sort... sorts ) {

        if (list.size()>1) {
            Sorting.sort(list, sorts);

            return list.get(list.size()-1);
        } else {
            return null;
        }
    }

public static < T > T lastOf ( List < T > list , Sort ... sorts ) { if ( list . size ( ) > 1 ) { Sorting . sort ( list , sorts ) ; return list . get ( list . size ( ) - 1 ) ; } else { return null ; } }

Grabs the last items after the sort.
@param list
@param sorts
@param <T>
@return


public static <T> List<T> lastOf( List<T> list, int count, Sort... sorts ) {

        if (list.size()>1) {
            Sorting.sort(list, sorts);

            return Lists.endSliceOf(list, count *-1);
        } else {
            return null;
        }
    }

public static < T > List < T > lastOf ( List < T > list , int count , Sort ... sorts ) { if ( list . size ( ) > 1 ) { Sorting . sort ( list , sorts ) ; return Lists . endSliceOf ( list , count * - 1 ) ; } else { return null ; } }

Grabs the last few items from the list.
@param list
@param count
@param sorts
@param <T>
@return

Grabs the last few items from the list .


public static <T> List<T> least( List<T> list, int count ) {

        if (list.size()>1) {
            Sorting.sort(list);

            return Lists.sliceOf(list, 0, count);
        } else {
            return null;
        }
    }

public static < T > List < T > least ( List < T > list , int count ) { if ( list . size ( ) > 1 ) { Sorting . sort ( list ) ; return Lists . sliceOf ( list , 0 , count ) ; } else { return null ; } }

Returns the least few.
@param list
@param count
@param <T>
@return


public static <T> T min( List<T> list ) {

        if (list.size()>1) {
            Sorting.sort(list);

            return list.get(0);
        } else {
            return null;
        }
    }

public static < T > T min ( List < T > list ) { if ( list . size ( ) > 1 ) { Sorting . sort ( list ) ; return list . get ( 0 ) ; } else { return null ; } }

Returns the min value using a natural sort.
@param list
@param <T>
@return

Returns the min value using a natural sort .


public static <T> T min( T[] array, String sortBy ) {

        if ( array.length > 1 ) {
            Sorting.sort(array, sortBy);

            return array[0];
        } else {
            return null;
        }
    }

public static < T > T min ( T [ ] array , String sortBy ) { if ( array . length > 1 ) { Sorting . sort ( array , sortBy ) ; return array [ 0 ] ; } else { return null ; } }

Returns the min value of the object with the property given.

@param array
@param sortBy
@param <T>
@return

Returns the min value of the object with the property given .


private  void processArrayOfMaps( Object newInstance, FieldAccess field, Map<String, Object>[] maps) {
        List<Map<String, Object>> list = Lists.list(maps);
        handleCollectionOfMaps(  newInstance, field,
                list);

    }

private void processArrayOfMaps ( Object newInstance , FieldAccess field , Map < String , Object > [ ] maps ) { List < Map < String , Object > > list = Lists . list ( maps ) ; handleCollectionOfMaps ( newInstance , field , list ) ; }

Processes an array of maps.
@param newInstance  new instance we are injecting field into
@param field    field we are injecting a value into


@SuppressWarnings("unchecked")
    private  void handleCollectionOfMaps( Object newInstance,
                                          FieldAccess field, Collection<Map<String, Object>> collectionOfMaps
    ) {

        Collection<Object> newCollection = Conversions.createCollection( field.type(), collectionOfMaps.size() );

        Class<?> componentClass = field.getComponentClass();

        if ( componentClass != null ) {

            for ( Map<String, Object> mapComponent : collectionOfMaps ) {

                newCollection.add( fromMap( mapComponent, componentClass ) );

            }
            field.setObject( newInstance, newCollection );

        }

    }

@ SuppressWarnings ( "unchecked" ) private void handleCollectionOfMaps ( Object newInstance , FieldAccess field , Collection < Map < String , Object > > collectionOfMaps ) { Collection < Object > newCollection = Conversions . createCollection ( field . type ( ) , collectionOfMaps . size ( ) ) ; Class < ? > componentClass = field . getComponentClass ( ) ; if ( componentClass != null ) { for ( Map < String , Object > mapComponent : collectionOfMaps ) { newCollection . add ( fromMap ( mapComponent , componentClass ) ) ; } field . setObject ( newInstance , newCollection ) ; } }

Processes an collection of maps.
@param newInstance  new instance we are injecting field into
@param field    field we are injecting a value into


@Override
    public  Object fromMap(Map<String, Object> map) {
        String clazz = (String) map.get( "class" );
        Class cls = Reflection.loadClass( clazz );
        return fromMap(map, cls);
    }

@ Override public Object fromMap ( Map < String , Object > map ) { String clazz = ( String ) map . get ( "class" ) ; Class cls = Reflection . loadClass ( clazz ) ; return fromMap ( map , cls ) ; }

fromMap converts a map into a Java object.
This version will see if there is a class parameter in the map, and dies if there is not.
@param map map to create the object from.
@return new object

fromMap converts a map into a Java object . This version will see if there is a class parameter in the map and dies if there is not .


@Override
    public VALUE get( KEY key ) {
        removeThenAddKey( key );
        return map.get( key );
    }

@ Override public VALUE get ( KEY key ) { removeThenAddKey ( key ) ; return map . get ( key ) ; }

Get the value at key
@param key the key
@return value


public static String createToolTipWithNameSpace( final String namespace, final String fieldName,
                                                     final ResourceBundle bundle, final String toolTipType ) {

        String toolTip = null;
        try {
            try {
                /** Look for name-space + . + fieldName, e.g., Employee.firstName.toolTip. */
                toolTip = bundle.getString( namespace + '.' + fieldName + '.'
                        + toolTipType );
            } catch ( MissingResourceException mre ) {
                /** Look for fieldName only, e.g., firstName.toolTip. */
                toolTip = bundle.getString( fieldName + '.' + toolTipType );
            }
        } catch ( MissingResourceException mre ) {
        }

        return toolTip;
    }

Get the tool tip.

@param namespace Namespace is a qualifier usually the name of a Form or Entity.
@param fieldName fieldName is the name of the tool tip we want to look up.
@param bundle    bundle
@return Tool Tip from the Message Source.


public static String generateLabelValue( final String fieldName ) {

        final StringBuilder buffer = new StringBuilder( fieldName.length() * 2 );

        class GenerationCommand {
            boolean capNextChar = false;
            boolean lastCharWasUpperCase = false;
            boolean lastCharWasNumber = false;
            boolean lastCharWasSpecial = false;
            boolean shouldContinue = true;
            char[] chars = fieldName.toCharArray();

            void processFieldName() {

                for ( int index = 0; index < chars.length; index++ ) {
                    char cchar = chars[ index ];
                    shouldContinue = true;

                    processCharWasNumber( buffer, index, cchar );
                    if ( !shouldContinue ) {
                        continue;
                    }

                    processCharWasUpperCase( buffer, index, cchar );
                    if ( !shouldContinue ) {
                        continue;
                    }

                    processSpecialChars( buffer, cchar );

                    if ( !shouldContinue ) {
                        continue;
                    }

                    cchar = processCapitalizeCommand( cchar );

                    cchar = processFirstCharacterCheck( buffer, index, cchar );

                    if ( !shouldContinue ) {
                        continue;
                    }

                    buffer.append( cchar );
                }

            }

            private void processCharWasNumber( StringBuilder buffer,
                                               int index, char cchar ) {
                if ( lastCharWasSpecial ) {
                    return;
                }

                if ( Character.isDigit( cchar ) ) {

                    if ( index != 0 && !lastCharWasNumber ) {
                        buffer.append( ' ' );
                    }

                    lastCharWasNumber = true;
                    buffer.append( cchar );

                    this.shouldContinue = false;
                } else {
                    lastCharWasNumber = false;
                }
            }

            private char processFirstCharacterCheck( final StringBuilder buffer,
                                                     int index, char cchar ) {
                /* Always capitalize the first character. */
                if ( index == 0 ) {
                    cchar = Character.toUpperCase( cchar );
                    buffer.append( cchar );
                    this.shouldContinue = false;
                }
                return cchar;
            }

            private char processCapitalizeCommand( char cchar ) {
                /* Capitalize the character. */
                if ( capNextChar ) {
                    capNextChar = false;
                    cchar = Character.toUpperCase( cchar );
                }
                return cchar;
            }

            private void processSpecialChars( final StringBuilder buffer,
                                              char cchar ) {
                lastCharWasSpecial = false;
                /* If the character is '.' or '_' then append a space and mark
                 * the next iteration to capitalize.
				 */
                if ( cchar == '.' || cchar == '_' ) {
                    buffer.append( ' ' );
                    capNextChar = true;
                    lastCharWasSpecial = false;
                    this.shouldContinue = false;
                }

            }

            private void processCharWasUpperCase( final StringBuilder buffer,
                                                  int index, char cchar ) {
                /* If the character is uppercase, append a space and keep track
                 * that the last character was uppercase for the next iteration.
				 */
                if ( Character.isUpperCase( cchar ) ) {

                    if ( index != 0 && !lastCharWasUpperCase ) {
                        buffer.append( ' ' );
                    }

                    lastCharWasUpperCase = true;
                    buffer.append( cchar );

                    this.shouldContinue = false;
                } else {
                    lastCharWasUpperCase = false;
                }
            }
        }

        GenerationCommand gc = new GenerationCommand();
        gc.processFieldName();

        /* This is a hack to get address.line_1 to work. */
        return buffer.toString().replace( "  ", " " );
    }

Generate the field. Transforms firstName into First Name. This allows
reasonable defaults for labels.

@param fieldName fieldName
@return generated label name.

Generate the field . Transforms firstName into First Name . This allows reasonable defaults for labels .


public  CharSequence addHex( final int decoded  ) {

        int _location = location;
        char [] _buffer = buffer;
        int _capacity = capacity;

        if ( 2 + _location > _capacity ) {
            _buffer = Chr.grow( _buffer );
            _capacity = _buffer.length;
        }

        _buffer [_location] = (char) encodeNibbleToHexAsciiCharByte( ( decoded >> 4 ) & 0x0F );
        _location ++;

        _buffer [_location] = (char) encodeNibbleToHexAsciiCharByte( decoded & 0x0F );;
        _location ++;

        location = _location;
        buffer = _buffer;
        capacity = _capacity;
        return this;
    }

public CharSequence addHex ( final int decoded ) { int _location = location ; char [ ] _buffer = buffer ; int _capacity = capacity ; if ( 2 + _location > _capacity ) { _buffer = Chr . grow ( _buffer ) ; _capacity = _buffer . length ; } _buffer [ _location ] = ( char ) encodeNibbleToHexAsciiCharByte ( ( decoded >> 4 ) & 0x0F ) ; _location ++ ; _buffer [ _location ] = ( char ) encodeNibbleToHexAsciiCharByte ( decoded & 0x0F ) ; ; _location ++ ; location = _location ; buffer = _buffer ; capacity = _capacity ; return this ; }

Turn a single bytes into two hex character representation.

@param decoded the byte to serializeObject.

Turn a single bytes into two hex character representation .


private void processReadQueue() throws InterruptedException {

        ReadStatus readStatus = new ReadStatus();

        while (true) {

            DataStoreRequest request = readOperationsQueue.poll(dataStoreConfig.pollTimeoutMS(), TimeUnit.MILLISECONDS);

            while (request != null) {

                readStatus.tracker.addCall(request, outputDataQueue);
                readOperationsBatch.add(request);
                if (readOperationsBatch.size() > dataStoreConfig.processQueueMaxBatchSize()) {
                    break;
                }
                request = readOperationsQueue.poll();

            }

            if (readOperationsBatch.size() > 0) {

                try {
                    recievedReadBatch(new ArrayList<>(readOperationsBatch));
                } finally {
                    readOperationsBatch.clear();
                }
            } else {
                flushReadsIfNeeded();
            }

            if (readStatus.readBatchSize.size() > 1_000) {

                StatCount count;
                final long now = Timer.timer().time();

                count = new StatCount(now, DataStoreSource.SERVER, Action.GET_STATS, "Thread TIME USER  BaseDataStore "
                        + Thread.currentThread().getName(), Sys.threadUserTime());
                this.outputDataQueue.put(count);

                count = new StatCount(now, DataStoreSource.SERVER, Action.GET_STATS, "Thread TIME CPU  BaseDataStore "
                        + Thread.currentThread().getName(), Sys.threadCPUTime());
                this.outputDataQueue.put(count);

                count = new StatCount(now, source, Action.GET, "BaseDataStore readStatus.readBatchSize.max", readStatus.readBatchSize.max());
                outputDataQueue.put(count);

                count = new StatCount(now, source, Action.GET, "BaseDataStore readStatus.readBatchSize.min", readStatus.readBatchSize.min());
                outputDataQueue.put(count);

                count = new StatCount(now, source, Action.GET, "BaseDataStore readStatus.readBatchSize.median", readStatus.readBatchSize.median());
                outputDataQueue.put(count);

                count = new StatCount(now, source, Action.GET, "BaseDataStore readStatus.readBatchSize.mean", readStatus.readBatchSize.mean());
                outputDataQueue.put(count);

                count = new StatCount(now, source, Action.GET, "BaseDataStore readStatus.readBatchSize.standardDeviation", readStatus.readBatchSize.standardDeviation());
                outputDataQueue.put(count);

                count = new StatCount(now, source, Action.GET, "BaseDataStore readStatus.readBatchSize.variance", readStatus.readBatchSize.variance());
                outputDataQueue.put(count);

                readStatus.readBatchSize.clear();

            }
        }

    }

Called from worker thread.

@throws InterruptedException


private void processWriteQueue() throws InterruptedException {

        WriteStatus status = new WriteStatus();

        while (true) {

            DataStoreRequest operation = writeOperationsQueue.poll(dataStoreConfig.pollTimeoutMS(), TimeUnit.MILLISECONDS);

            while (operation != null) {
                status.tracker.addCall(operation, outputDataQueue);
                writeOperationsBatch.add(operation);
                if (writeOperationsBatch.size() > dataStoreConfig.processQueueMaxBatchSize()) {
                    break;
                }
                operation = writeOperationsQueue.poll();

            }

            if (writeOperationsBatch.size() > 0) {

                try {
                    status.writeBatchSize.add(writeOperationsBatch.size());
                    recievedWriteBatch(new ArrayList<>(writeOperationsBatch));
                } finally {
                    writeOperationsBatch.clear();
                }
            } else {
                flushWritesIfNeeded();
            }

            if (status.writeBatchSize.size() > 1000) {

                status.sendBatchSize(source, outputDataQueue);
            }
        }
    }

private void processWriteQueue ( ) throws InterruptedException { WriteStatus status = new WriteStatus ( ) ; while ( true ) { DataStoreRequest operation = writeOperationsQueue . poll ( dataStoreConfig . pollTimeoutMS ( ) , TimeUnit . MILLISECONDS ) ; while ( operation != null ) { status . tracker . addCall ( operation , outputDataQueue ) ; writeOperationsBatch . add ( operation ) ; if ( writeOperationsBatch . size ( ) > dataStoreConfig . processQueueMaxBatchSize ( ) ) { break ; } operation = writeOperationsQueue . poll ( ) ; } if ( writeOperationsBatch . size ( ) > 0 ) { try { status . writeBatchSize . add ( writeOperationsBatch . size ( ) ) ; recievedWriteBatch ( new ArrayList <> ( writeOperationsBatch ) ) ; } finally { writeOperationsBatch . clear ( ) ; } } else { flushWritesIfNeeded ( ) ; } if ( status . writeBatchSize . size ( ) > 1000 ) { status . sendBatchSize ( source , outputDataQueue ) ; } } }

Called from worker thread.
Processes the incoming queue for read and writes.

Called from worker thread . Processes the incoming queue for read and writes .


public void start() {

        scheduledExecutorService = Executors.newScheduledThreadPool(2,
                new ThreadFactory() {
                    @Override
                    public Thread newThread(Runnable runnable) {
                        Thread thread = new Thread(runnable);
                        thread.setName(" DataQueue Process " + source);
                        return thread;
                    }
                }
        );

        future = scheduledExecutorService.scheduleAtFixedRate(new Runnable() {

            @Override
            public void run() {

                if (stop.get()) {
                    return;
                }
                try {
                    processWriteQueue();
                } catch (InterruptedException ex) {
                    //let it restart or stop

                } catch (Exception ex) {
                    logger.fatal(ex);
                }
            }
        }, 0, dataStoreConfig.threadErrorResumeTimeMS(), TimeUnit.MILLISECONDS);

        future = scheduledExecutorService.scheduleAtFixedRate(new Runnable() {

            @Override
            public void run() {

                if (stop.get()) {
                    return;
                }
                try {
                    processReadQueue();
                } catch (InterruptedException ex) {
                    //let it restart or stop

                } catch (Exception ex) {
                    logger.fatal(ex, "Problem with base data store running scheduled job");
                }
            }
        }, 0, dataStoreConfig.threadErrorResumeTimeMS(), TimeUnit.MILLISECONDS);

    }


@Universal
    public static String atIndex( String str, int index, char c ) {
            return idx (str, index, c);
    }

@ Universal public static String atIndex ( String str , int index , char c ) { return idx ( str , index , c ) ; }

Puts character at index
@param str string
@param index index
@param c char to put in
@return new string


@Universal
    public static String slc( String str, int start ) {

        return FastStringUtils.noCopyStringFromChars( Chr.slc( FastStringUtils.toCharArray(str), start ) );
    }

@ Universal public static String slc ( String str , int start ) { return FastStringUtils . noCopyStringFromChars ( Chr . slc ( FastStringUtils . toCharArray ( str ) , start ) ) ; }

Gets slice of a string.
@param str string
@param start start index of slice
@return new string


@Universal
    public static boolean in( char[] chars, String str ) {
        return Chr.in ( chars, FastStringUtils.toCharArray(str) );
    }

@ Universal public static boolean in ( char [ ] chars , String str ) { return Chr . in ( chars , FastStringUtils . toCharArray ( str ) ) ; }

See if chars is in another string
@param chars chars
@param str string
@return true or false


@Universal
    public static String add( String str, char c ) {
        return FastStringUtils.noCopyStringFromChars( Chr.add( FastStringUtils.toCharArray(str), c ) );
    }

@ Universal public static String add ( String str , char c ) { return FastStringUtils . noCopyStringFromChars ( Chr . add ( FastStringUtils . toCharArray ( str ) , c ) ) ; }

Add a char to a string
@param str string
@param c char
@return new string


public static String addObjects( Object... objects ) {
        int length = 0;
        for ( Object obj : objects ) {
            if ( obj == null ) {
                continue;
            }
            length += obj.toString().length();
        }
        CharBuf builder = CharBuf.createExact( length );
        for ( Object str : objects ) {
            if ( str == null ) {
                continue;
            }
            builder.add( str.toString() );
        }
        return builder.toString();
    }

public static String addObjects ( Object ... objects ) { int length = 0 ; for ( Object obj : objects ) { if ( obj == null ) { continue ; } length += obj . toString ( ) . length ( ) ; } CharBuf builder = CharBuf . createExact ( length ) ; for ( Object str : objects ) { if ( str == null ) { continue ; } builder . add ( str . toString ( ) ) ; } return builder . toString ( ) ; }

Add many objects converted to strings together.
Null are ignored so be careful.
@param objects objects to convert to strings
@return new string

Add many objects converted to strings together . Null are ignored so be careful .


public static String compact( String str ) {
        return FastStringUtils.noCopyStringFromChars( Chr.compact( FastStringUtils.toCharArray(str) ) );
    }

public static String compact ( String str ) { return FastStringUtils . noCopyStringFromChars ( Chr . compact ( FastStringUtils . toCharArray ( str ) ) ) ; }

Gets rid of null characters lurking in the string
@param str string
@return new string


public static String[] split( String str ) {
        char[][] split = Chr.split( FastStringUtils.toCharArray(str) );
        return fromCharArrayOfArrayToStringArray( split );
    }

public static String [ ] split ( String str ) { char [ ] [ ] split = Chr . split ( FastStringUtils . toCharArray ( str ) ) ; return fromCharArrayOfArrayToStringArray ( split ) ; }

Split a string
@param str string to split
@return string array


public static String[] splitBySpace( String str ) {
        char[][] split = CharScanner.splitBySpace( FastStringUtils.toCharArray(str) );
        return fromCharArrayOfArrayToStringArray( split );
    }

public static String [ ] splitBySpace ( String str ) { char [ ] [ ] split = CharScanner . splitBySpace ( FastStringUtils . toCharArray ( str ) ) ; return fromCharArrayOfArrayToStringArray ( split ) ; }

Split a string by space
@param str string to split
@return string array


public static String[] splitByPipe( String str ) {
        char[][] split = CharScanner.splitByPipe( FastStringUtils.toCharArray(str) );
        return fromCharArrayOfArrayToStringArray( split );
    }

public static String [ ] splitByPipe ( String str ) { char [ ] [ ] split = CharScanner . splitByPipe ( FastStringUtils . toCharArray ( str ) ) ; return fromCharArrayOfArrayToStringArray ( split ) ; }

Split a string by pipe
@param str string to split
@return string array


public static String[] fromCharArrayOfArrayToStringArray( char[][] split ) {
        String[] results = new String[ split.length ];

        char[] array;

        for ( int index = 0; index < split.length; index++ ) {
            array = split[ index ];

            results[ index ] = array.length == 0 ?
                    EMPTY_STRING : FastStringUtils.noCopyStringFromChars( array );
        }
        return results;
    }

public static String [ ] fromCharArrayOfArrayToStringArray ( char [ ] [ ] split ) { String [ ] results = new String [ split . length ] ; char [ ] array ; for ( int index = 0 ; index < split . length ; index ++ ) { array = split [ index ] ; results [ index ] = array . length == 0 ? EMPTY_STRING : FastStringUtils . noCopyStringFromChars ( array ) ; } return results ; }

Convert arrays of chars to arrays of strings
@param split array of chars
@return string array


public static String camelCase( String inStr, boolean upper ) {
        char[] in = FastStringUtils.toCharArray(inStr);
        char[] out = Chr.camelCase( in, upper );
        return FastStringUtils.noCopyStringFromChars( out );
    }

public static String camelCase ( String inStr , boolean upper ) { char [ ] in = FastStringUtils . toCharArray ( inStr ) ; char [ ] out = Chr . camelCase ( in , upper ) ; return FastStringUtils . noCopyStringFromChars ( out ) ; }

Convert to camel case and pass upper or lower
@param inStr string to convert
@param upper upper flag
@return new string


public static boolean insideOf(String start, String inStr, String end) {
        return Chr.insideOf(FastStringUtils.toCharArray(start), FastStringUtils.toCharArray(inStr), FastStringUtils.toCharArray(end));
    }

public static boolean insideOf ( String start , String inStr , String end ) { return Chr . insideOf ( FastStringUtils . toCharArray ( start ) , FastStringUtils . toCharArray ( inStr ) , FastStringUtils . toCharArray ( end ) ) ; }

Checks to see if a string is inside of another
@param start start
@param inStr input string
@param end index at end
@return


public static String underBarCase( String inStr ) {
        char[] in = FastStringUtils.toCharArray(inStr);
        char[] out = Chr.underBarCase( in );
        return FastStringUtils.noCopyStringFromChars( out );
    }

public static String underBarCase ( String inStr ) { char [ ] in = FastStringUtils . toCharArray ( inStr ) ; char [ ] out = Chr . underBarCase ( in ) ; return FastStringUtils . noCopyStringFromChars ( out ) ; }

Convert to under bar case
@param inStr input string
@return new string


public static String num(Number count) {

        if (count == null) {
            return "";
        }
        if (count instanceof  Double || count instanceof BigDecimal) {
            String s = count.toString();
            if (idx(s, 1) == '.' && s.length() > 7) {
                s = slc(s, 0, 5);
                return s;
            } else {
                return s;
            }

        } else if (count instanceof Integer || count instanceof Long || count instanceof Short || count instanceof BigInteger){
            String s = count.toString();
            s = new StringBuilder(s).reverse().toString();

            CharBuf buf = CharBuf.create(s.length());

            int index = 0;
            for (char c : s.toCharArray()) {

                index++;

                buf.add(c);

                if (index % 3 == 0) {
                    buf.add(',');
                }

            }

            if (buf.lastChar() == ',') {
                buf.removeLastChar();
            }

            s = buf.toString();

            s = new StringBuilder(s).reverse().toString();

            return s;

        }

        return count.toString();

    }

public static String num ( Number count ) { if ( count == null ) { return "" ; } if ( count instanceof Double || count instanceof BigDecimal ) { String s = count . toString ( ) ; if ( idx ( s , 1 ) == ' ' && s . length ( ) > 7 ) { s = slc ( s , 0 , 5 ) ; return s ; } else { return s ; } } else if ( count instanceof Integer || count instanceof Long || count instanceof Short || count instanceof BigInteger ) { String s = count . toString ( ) ; s = new StringBuilder ( s ) . reverse ( ) . toString ( ) ; CharBuf buf = CharBuf . create ( s . length ( ) ) ; int index = 0 ; for ( char c : s . toCharArray ( ) ) { index ++ ; buf . add ( c ) ; if ( index % 3 == 0 ) { buf . add ( ' ' ) ; } } if ( buf . lastChar ( ) == ' ' ) { buf . removeLastChar ( ) ; } s = buf . toString ( ) ; s = new StringBuilder ( s ) . reverse ( ) . toString ( ) ; return s ; } return count . toString ( ) ; }

Do a nice pretty print of a number.
Add commas and such.
@param count number
@return string format of number

Do a nice pretty print of a number . Add commas and such .


public static Sort sorts( Sort... sorts ) {
        if ( sorts == null || sorts.length == 0 ) {
            return null;
        }

        Sort main = sorts[ 0 ];
        for ( int index = 1; index < sorts.length; index++ ) {
            main.then( sorts[ index ] );
        }
        return main;
    }

public static Sort sorts ( Sort ... sorts ) { if ( sorts == null || sorts . length == 0 ) { return null ; } Sort main = sorts [ 0 ] ; for ( int index = 1 ; index < sorts . length ; index ++ ) { main . then ( sorts [ index ] ) ; } return main ; }

Helper method to create a Sort that is a composite of other sorts.
@param sorts list of child sorts
@return

Helper method to create a Sort that is a composite of other sorts .


public void sort( List list, Map<String, FieldAccess> fields ) {
        Collections.sort( list, this.comparator( fields ) );
    }

public void sort ( List list , Map < String , FieldAccess > fields ) { Collections . sort ( list , this . comparator ( fields ) ) ; }

Sort if you already know the reflection fields.
@param list sort from list
@param fields sort from fields.

Sort if you already know the reflection fields .


public Comparator comparator( Map<String, FieldAccess> fields ) {
        if ( comparator == null ) {
            comparator = universalComparator(this.getName(), fields,
                    this.getType(), this.childComparators(fields));
        }
        return comparator;
    }

public Comparator comparator ( Map < String , FieldAccess > fields ) { if ( comparator == null ) { comparator = universalComparator ( this . getName ( ) , fields , this . getType ( ) , this . childComparators ( fields ) ) ; } return comparator ; }

This is what really does the magic. This is the comparator creator.

This is what really does the magic . This is the comparator creator .


private List<Comparator> childComparators( Map<String, FieldAccess> fields ) {
        if ( this.comparators == null ) {
            this.comparators = new ArrayList<>( this.sorts.size() + 1 );

            for ( Sort sort : sorts ) {
                Comparator comparator = universalComparator(
                        sort.getName(),
                        fields,
                        sort.getType(),
                        sort.childComparators(fields)
                );
                this.comparators.add( comparator );
            }
        }
        return this.comparators;
    }

private List < Comparator > childComparators ( Map < String , FieldAccess > fields ) { if ( this . comparators == null ) { this . comparators = new ArrayList <> ( this . sorts . size ( ) + 1 ) ; for ( Sort sort : sorts ) { Comparator comparator = universalComparator ( sort . getName ( ) , fields , sort . getType ( ) , sort . childComparators ( fields ) ) ; this . comparators . add ( comparator ) ; } } return this . comparators ; }

This creates a list of children comparators based on the child list.
@param fields
@return

This creates a list of children comparators based on the child list .


public static <K, V> boolean valueIn( V value, Map<K, V> map ) {
        return map.containsValue( value );
    }

public static < K , V > boolean valueIn ( V value , Map < K , V > map ) { return map . containsValue ( value ) ; }


public static boolean equalsOrDie(int expected, int got) {
        if (expected != got) {
            return die(Boolean.class, "Expected was", expected, "but we got ", got);
        }
        return true;
    }

public
static
boolean
equalsOrDie
(
int
expected
,
int
got
)
{
if
(
expected
!=
got
)
{
return
die
(
Boolean
.
class
,
"Expected was"
,
expected
,
"but we got "
,
got
)
;
}
return
true
;
}

Checks to see if two values are the same
@param expected expected value
@param got got value
@return true if equal throws exception if not equal


public static boolean equalsOrDie(int[] expected, int[] got) {

        if (expected.length != got.length) {
            die("Lengths did not match, expected length", expected.length,
                    "but got", got.length);
        }

        for (int index=0; index< expected.length; index++) {
            if (expected[index]!= got[index]) {
                die("value at index did not match index", index , "expected value",
                        expected[index],
                        "but got", got[index]);

            }
        }
        return true;
    }

Checks to see if two arrays are equals
@param expected expected array
@param got got array
@return true if equal or throws exception if not.


public static int sum( int[] values, int start, int length ) {
        long sum = 0;
        for (int index = start; index < length; index++ ) {
            sum+= values[index];
        }

        if (sum < Integer.MIN_VALUE) {
            die ("overflow the sum is too small", sum);
        }

        if (sum > Integer.MAX_VALUE) {
            die ("overflow the sum is too big", sum);
        }

        return (int) sum;

    }

public
static
int
sum
(
int
[
]
values
,
int
start
,
int
length
)
{
long
sum
=
0
;
for
(
int
index
=
start
;
index
<
length
;
index
++
)
{
sum
+=
values
[
index
]
;
}
if
(
sum
<
Integer
.
MIN_VALUE
)
{
die
(
"overflow the sum is too small"
,
sum
)
;
}
if
(
sum
>
Integer
.
MAX_VALUE
)
{
die
(
"overflow the sum is too big"
,
sum
)
;
}
return
(
int
)
sum
;
}

Sum
Provides overflow protection.
@param values values in int
@return sum


public static int roundUpToPowerOf2( int number ) {
        int rounded = number >= 1_000
                ? 1_000
                : ( rounded = Integer.highestOneBit( number ) ) != 0
                ? ( Integer.bitCount( number ) > 1 ) ? rounded << 1 : rounded
                : 1;

        return rounded;
    }

public static int roundUpToPowerOf2 ( int number ) { int rounded = number >= 1_000 ? 1_000 : ( rounded = Integer . highestOneBit ( number ) ) != 0 ? ( Integer . bitCount ( number ) > 1 ) ? rounded << 1 : rounded : 1 ; return rounded ; }

Round up to the nearest power of 2
@param number number you want to round up to.
@return rounded up to the power of 2.


public static void sort( List list, String sortBy, Map<String, FieldAccess> fields, boolean ascending) {

        sort(list, sortBy, fields, ascending, false);

    }

public static void sort ( List list , String sortBy , Map < String , FieldAccess > fields , boolean ascending ) { sort ( list , sortBy , fields , ascending , false ) ; }

This is the work horse. It does all of the sorting work for the simple cases.
Nulls are last by default.
@param list the list you want to sort.
@param sortBy what you want to sort the list by.
@param fields the reflection fields
@param ascending true for ascending

This is the work horse . It does all of the sorting work for the simple cases . Nulls are last by default .


public static void sort( List list, String sortBy, Map<String, FieldAccess> fields, boolean ascending,
                             boolean nullsFirst) {

        try {

            /* If this list is null or empty, we have nothing to do so return. */
            if ( list == null || list.size() == 0 ) {
                return;
            }

            /* Grab the first item in the list and see what it is. */
            Object o = list.get( 0 );

            /* if the sort by string is is this, and the object is comparable then use the objects
            themselves for the sort.
             */
            if ( sortBy.equals( "this" )  ) {

                Collections.sort(list, thisUniversalComparator(ascending, nullsFirst));
                return;
            }

            /* If you did not sort by "this", then sort by the field. */

            final FieldAccess field = fields.get( sortBy );

            if ( field != null ) {

                Collections.sort( list, Sorting.universalComparator(field, ascending, nullsFirst) );

            }

        } catch (Exception ex) {
            Exceptions.handle(ex, "list", list, "\nsortBy", sortBy, "fields", fields, "ascending", ascending,
            "nullFirst", nullsFirst);
        }
    }

This is the work horse. It does all of the sorting work for the simple cases.
@param list the list you want to sort.
@param sortBy what you want to sort the list by.
@param fields the reflection fields
@param ascending true for ascending

This is the work horse . It does all of the sorting work for the simple cases .


private void setFieldValueFromMap( final Object parentObject,
                                       final FieldAccess field,  final Map mapInner ) {

        Class<?> fieldClassType = field.type();
        Object value = null;

        /* Is the field not a map. */
        if ( !Typ.isMap( fieldClassType ) )  {

            if ( !fieldClassType.isInterface() && !Typ.isAbstract( fieldClassType ) ) {
                value = fromMap(  mapInner, field.type() );

            } else {
                Object  oClassName =  mapInner.get( "class" );
                if (oClassName != null)  {
                    value = fromMap( mapInner, Reflection.loadClass( oClassName.toString() ));
                } else {
                    value = null;
                }
            }

           /*
           REFACTOR:
           This is at least the third time that I have seen this code in the class.
            It was either cut and pasted or I forgot I wrote it three times.
           REFACTOR:
             */
        }  else if (Typ.isMap( fieldClassType ))  {
            Class keyType = (Class)field.getParameterizedType().getActualTypeArguments()[0];
            Class valueType = (Class)field.getParameterizedType().getActualTypeArguments()[1];

            Set<Map.Entry> set = mapInner.entrySet();
            Map newMap = new LinkedHashMap(  );

            for (Map.Entry entry : set) {
                Object evalue = entry.getValue();

                Object key = entry.getKey();

                if (evalue instanceof ValueContainer) {
                    evalue = ((ValueContainer) evalue).toValue();
                }

                key  = Conversions.coerce(keyType, key);
                evalue = Conversions.coerce( valueType, evalue );
                newMap.put( key, evalue );
            }

            value  = newMap;

        }

        field.setValue(parentObject, value);

    }

Inject a map into an object's field.
@param field field we are injecting a value into


@Override
    public  List<?> toList(Object object) {

        TypeType instanceType = TypeType.getInstanceType(object);

        switch (instanceType) {
            case NULL:
                return Lists.list((Object)null);

            case ARRAY:
            case ARRAY_INT:
            case ARRAY_BYTE:
            case ARRAY_SHORT:
            case ARRAY_FLOAT:
            case ARRAY_DOUBLE:
            case ARRAY_LONG:
            case ARRAY_STRING:
            case ARRAY_OBJECT:
                return Conversions.toList(object);

            case INSTANCE:
                if (Reflection.respondsTo(object, "toList")) {
                    return (List<?>) Reflection.invoke(object, "toList");
                }
                break;
        }
        return Lists.list(object);
    }

@ Override public List < ? > toList ( Object object ) { TypeType instanceType = TypeType . getInstanceType ( object ) ; switch ( instanceType ) { case NULL : return Lists . list ( ( Object ) null ) ; case ARRAY : case ARRAY_INT : case ARRAY_BYTE : case ARRAY_SHORT : case ARRAY_FLOAT : case ARRAY_DOUBLE : case ARRAY_LONG : case ARRAY_STRING : case ARRAY_OBJECT : return Conversions . toList ( object ) ; case INSTANCE : if ( Reflection . respondsTo ( object , "toList" ) ) { return ( List < ? > ) Reflection . invoke ( object , "toList" ) ; } break ; } return Lists . list ( object ) ; }

Convert an object to a list.

@param object the object we want to convert to a list
@return new list from an object


protected void createLoadAllVersionDataSQL(String table) {

        CharBuf buf = CharBuf.create(100);
        buf.add("select kv_key, 1, version, update_timestamp, create_timestamp from `");
        buf.add(table);
        buf.add("` where kv_key in (");
        buf.multiply("?,", this.loadKeyCount);
        buf.removeLastChar();
        buf.add(");");

        this.loadAllVersionDataByKeysSQL = buf.toString();
    }

Create load all keys SQL.
@param table table


@Universal
    public static <V> List<V> deepCopy( List<V> list ) {
        if ( list instanceof LinkedList ) {
            return deepCopyToList( list, new LinkedList<V>(  ) );
        } else if ( list instanceof CopyOnWriteArrayList ) {
            return deepCopyToList( list, new CopyOnWriteArrayList<V>(  ));
        } else {
            return deepCopy( (Collection)list);
        }
    }

@ Universal public static < V > List < V > deepCopy ( List < V > list ) { if ( list instanceof LinkedList ) { return deepCopyToList ( list , new LinkedList < V > ( ) ) ; } else if ( list instanceof CopyOnWriteArrayList ) { return deepCopyToList ( list , new CopyOnWriteArrayList < V > ( ) ) ; } else { return deepCopy ( ( Collection ) list ) ; } }

Clones each list item into a new instance with copied fields.
It is like doing a clone operation.

If the passed list is a LinkedList then the returned list will be a
LinkedList.

If the passed list is a CopyOnWriteArrayList then the returned list will
be a CopyOnWriteArrayList list.

All other lists become ArrayList.

@param list list to clone
@param <V> generics
@return new list

Clones each list item into a new instance with copied fields . It is like doing a clone operation .


public static boolean hasStringField( final Object value1, final String name ) {

        Class<?> clz = value1.getClass();
        return classHasStringField( clz, name );
    }

public static boolean hasStringField ( final Object value1 , final String name ) { Class < ? > clz = value1 . getClass ( ) ; return classHasStringField ( clz , name ) ; }

Checks to see if we have a string field.

@param value1
@param name
@return

Checks to see if we have a string field .


public static boolean classHasStringField( Class<?> clz, String name ) {

        List<Field> fields = Reflection.getAllFields( clz );
        for ( Field field : fields ) {
            if (
                    field.getType().equals( Typ.string ) &&
                            field.getName().equals( name ) &&
                            !Modifier.isStatic( field.getModifiers() ) &&
                            field.getDeclaringClass() == clz
                    ) {
                return true;
            }
        }

        return false;
    }

public static boolean classHasStringField ( Class < ? > clz , String name ) { List < Field > fields = Reflection . getAllFields ( clz ) ; for ( Field field : fields ) { if ( field . getType ( ) . equals ( Typ . string ) && field . getName ( ) . equals ( name ) && ! Modifier . isStatic ( field . getModifiers ( ) ) && field . getDeclaringClass ( ) == clz ) { return true ; } } return false ; }

Checks to see if this class has a string field.

@param clz
@param name
@return

Checks to see if this class has a string field .


public static boolean classHasField( Class<?> clz, String name ) {
        List<Field> fields = Reflection.getAllFields( clz );
        for ( Field field : fields ) {
            if ( field.getName().equals( name )
                    && !Modifier.isStatic( field.getModifiers() )
                    && field.getDeclaringClass() == clz ) {
                return true;
            }
        }

        return false;
    }

public static boolean classHasField ( Class < ? > clz , String name ) { List < Field > fields = Reflection . getAllFields ( clz ) ; for ( Field field : fields ) { if ( field . getName ( ) . equals ( name ) && ! Modifier . isStatic ( field . getModifiers ( ) ) && field . getDeclaringClass ( ) == clz ) { return true ; } } return false ; }

Checks to see if a class has a field.

@param clz
@param name
@return


public static String getFirstComparableOrPrimitiveFromClass( Class<?> clz ) {
        List<Field> fields = Reflection.getAllFields( clz );
        for ( Field field : fields ) {

            if ( ( field.getType().isPrimitive() || Typ.isComparable( field.getType() )
                    && !Modifier.isStatic( field.getModifiers() )
                    && field.getDeclaringClass() == clz )
                    ) {
                return field.getName();
            }
        }

        return null;
    }

public static String getFirstComparableOrPrimitiveFromClass ( Class < ? > clz ) { List < Field > fields = Reflection . getAllFields ( clz ) ; for ( Field field : fields ) { if ( ( field . getType ( ) . isPrimitive ( ) || Typ . isComparable ( field . getType ( ) ) && ! Modifier . isStatic ( field . getModifiers ( ) ) && field . getDeclaringClass ( ) == clz ) ) { return field . getName ( ) ; } } return null ; }

This can be used for default sort.

@param clz class we are analyzing
@return first field that is comparable or primitive or null if not found.


public static String getSortableField( Object value1 ) {

        if (value1 instanceof Map) {
            return getSortableFieldFromMap( (Map<String, ?>) value1);
        } else {
            return getSortableFieldFromClass( value1.getClass() );
        }
    }

public static String getSortableField ( Object value1 ) { if ( value1 instanceof Map ) { return getSortableFieldFromMap ( ( Map < String , ? > ) value1 ) ; } else { return getSortableFieldFromClass ( value1 . getClass ( ) ) ; } }

Gets the first sortable fields found.

@param value1
@return sortable field


@Override
    public final int compareTo( CacheEntry other ) {
        switch ( type ) {

            case LFU:
                return compareToLFU( other );
            case LRU:
                return compareToLRU( other );
            case FIFO:
                return compareToFIFO( other );
            default:
                die();
                return 0;

        }
    }

@ Override public final int compareTo ( CacheEntry other ) { switch ( type ) { case LFU : return compareToLFU ( other ) ; case LRU : return compareToLRU ( other ) ; case FIFO : return compareToFIFO ( other ) ; default : die ( ) ; return 0 ; } }

Comparison of entries this determines what we will order the cache by
which determines which type of cache it is.
@param other the other entry
@return results

Comparison of entries this determines what we will order the cache by which determines which type of cache it is .


private final int compareTime( CacheEntry other ) {

        if ( time > other.time ) {  //this time stamp is  greater so it has higher priority
            return 1;
        } else if ( time < other.time ) {//this time stamp is lower so it has lower priority
            return -1;
        } else if ( time == other.time ) {//equal priority
            return 0;
        }
        die();
        return 0;
    }

Compare the time.
@param other the other entry
@return results


public static void sort(List list, Sort... sorts) {
        Sort.sorts(sorts).sort(list);
    }

public static void sort ( List list , Sort ... sorts ) { Sort . sorts ( sorts ) . sort ( list ) ; }

Takes a list an an array or sorts

@param list list to sorts
@param sorts what you want to sore the list by


public static void sort( List list, String sortBy, boolean ascending, boolean nullsFirst ) {
        if ( list == null || list.size() == 0 ) {
            return;
        }

        if (sortBy.equals("this")) {

            Collections.sort(list, thisUniversalComparator(ascending, nullsFirst));
            return;
        }
        Iterator iterator = list.iterator();
        Object object = iterator.next();

        Map<String, FieldAccess> fields = null;

        if (object != null) {
          fields = BeanUtils.getFieldsFromObject( object );
        } else {
            while(iterator.hasNext()) {

                object = iterator.next();
                if (object!=null) {
                    fields = BeanUtils.getFieldsFromObject( object );
                    break;
                }
            }

        }

        if (fields!=null) {

            final FieldAccess field = fields.get( sortBy );

            if ( field != null ) {

                Collections.sort( list, Sorting.universalComparator(field, ascending, nullsFirst) );

            }
        }
    }

public static void sort ( List list , String sortBy , boolean ascending , boolean nullsFirst ) { if ( list == null || list . size ( ) == 0 ) { return ; } if ( sortBy . equals ( "this" ) ) { Collections . sort ( list , thisUniversalComparator ( ascending , nullsFirst ) ) ; return ; } Iterator iterator = list . iterator ( ) ; Object object = iterator . next ( ) ; Map < String , FieldAccess > fields = null ; if ( object != null ) { fields = BeanUtils . getFieldsFromObject ( object ) ; } else { while ( iterator . hasNext ( ) ) { object = iterator . next ( ) ; if ( object != null ) { fields = BeanUtils . getFieldsFromObject ( object ) ; break ; } } } if ( fields != null ) { final FieldAccess field = fields . get ( sortBy ) ; if ( field != null ) { Collections . sort ( list , Sorting . universalComparator ( field , ascending , nullsFirst ) ) ; } } }

Sort a list.
@param list the list you want to sort
@param sortBy what you want to sort the list by
@param ascending do you want ascending order
@param nullsFirst do you want nulls first


public static <K, V> Collection<Map.Entry<K, V>>  sortEntries( Class<V> componentType, Map<K, V> map,
                                                            String sortBy, boolean ascending, boolean nullsFirst ) {

        return sort ((Class) componentType, (Collection) map.entrySet() , sortBy, ascending, nullsFirst);

    }

public static < K , V > Collection < Map . Entry < K , V > > sortEntries ( Class < V > componentType , Map < K , V > map , String sortBy , boolean ascending , boolean nullsFirst ) { return sort ( ( Class ) componentType , ( Collection ) map . entrySet ( ) , sortBy , ascending , nullsFirst ) ; }

Sort map entries.
@param map  the map entries you want to sort
@param sortBy what you want to sort the list by
@param ascending do you want ascending order
@param nullsFirst do you want nulls first


public static <K, V> Collection<Map.Entry<K, V>>  sortValues( Class<V> componentType, Map<K, V> map,
                                                                   String sortBy, boolean ascending, boolean nullsFirst ) {

        return sort ((Class) componentType, (Collection) map.values() , sortBy, ascending, nullsFirst);

    }

public static < K , V > Collection < Map . Entry < K , V > > sortValues ( Class < V > componentType , Map < K , V > map , String sortBy , boolean ascending , boolean nullsFirst ) { return sort ( ( Class ) componentType , ( Collection ) map . values ( ) , sortBy , ascending , nullsFirst ) ; }

Sort map values.
@param map  the map entries you want to sort
@param sortBy what you want to sort the list by
@param ascending do you want ascending order
@param nullsFirst do you want nulls first


public static <K, V> Collection<Map.Entry<K, V>>  sortKeys( Class<V> componentType, Map<K, V> map,
                                                                  String sortBy, boolean ascending, boolean nullsFirst ) {

        return sort ((Class) componentType, (Collection) map.keySet() , sortBy, ascending, nullsFirst);

    }

public static < K , V > Collection < Map . Entry < K , V > > sortKeys ( Class < V > componentType , Map < K , V > map , String sortBy , boolean ascending , boolean nullsFirst ) { return sort ( ( Class ) componentType , ( Collection ) map . keySet ( ) , sortBy , ascending , nullsFirst ) ; }

Sort map keys.
@param map  the map entries you want to sort
@param sortBy what you want to sort the list by
@param ascending do you want ascending order
@param nullsFirst do you want nulls first


public static <T> void  sort( T[] array, String sortBy, boolean ascending, boolean nullsFirst ) {
        if ( array == null || array.length == 0 ) {
            return;
        }

        if (sortBy.equals("this")) {

            Arrays.sort(array, thisUniversalComparator(ascending, nullsFirst));
            return;
        }

        Object object = array[0];

        Map<String, FieldAccess> fields = null;

        if (object != null) {
            fields = BeanUtils.getFieldsFromObject( object );
        } else {
            for (int index=1; index< array.length; index++) {

                object = array[index];
                if (object!=null) {
                    fields = BeanUtils.getFieldsFromObject( object );
                    break;
                }
            }

        }

        if (fields!=null) {

            final FieldAccess field = fields.get( sortBy );

            if ( field != null ) {

                Arrays.sort( array, Sorting.universalComparator(field, ascending, nullsFirst) );

            }
        }
    }

public static < T > void sort ( T [ ] array , String sortBy , boolean ascending , boolean nullsFirst ) { if ( array == null || array . length == 0 ) { return ; } if ( sortBy . equals ( "this" ) ) { Arrays . sort ( array , thisUniversalComparator ( ascending , nullsFirst ) ) ; return ; } Object object = array [ 0 ] ; Map < String , FieldAccess > fields = null ; if ( object != null ) { fields = BeanUtils . getFieldsFromObject ( object ) ; } else { for ( int index = 1 ; index < array . length ; index ++ ) { object = array [ index ] ; if ( object != null ) { fields = BeanUtils . getFieldsFromObject ( object ) ; break ; } } } if ( fields != null ) { final FieldAccess field = fields . get ( sortBy ) ; if ( field != null ) { Arrays . sort ( array , Sorting . universalComparator ( field , ascending , nullsFirst ) ) ; } } }

Sort an array.
@param array the list you want to sort
@param sortBy what you want to sort the list by
@param ascending do you want ascending order
@param nullsFirst do you want nulls first


public static Comparator universalComparator( final FieldAccess field, final boolean ascending,
                                                  final boolean nullsFirst) {
        return new Comparator() {
            @Override
            public int compare( Object o1, Object o2 ) {
                Object value1 = null;
                Object value2 = null;

                if ( ascending ) {
                    value1 = field.getValue( o1 );
                    value2 = field.getValue( o2 );
                } else {
                    value1 = field.getValue( o2 );
                    value2 = field.getValue( o1 );
                }
                return Sorting.compare(value1, value2, nullsFirst);
            }
        };
    }

public static Comparator universalComparator ( final FieldAccess field , final boolean ascending , final boolean nullsFirst ) { return new Comparator ( ) { @ Override public int compare ( Object o1 , Object o2 ) { Object value1 = null ; Object value2 = null ; if ( ascending ) { value1 = field . getValue ( o1 ) ; value2 = field . getValue ( o2 ) ; } else { value1 = field . getValue ( o2 ) ; value2 = field . getValue ( o1 ) ; } return Sorting . compare ( value1 , value2 , nullsFirst ) ; } } ; }

This creates the universal comparator object which is used by the sort work horse.

@param field The field we are sorting on.
@param ascending if this should be ascending or descending.
@return

This creates the universal comparator object which is used by the sort work horse .


public static Comparator thisUniversalComparator( final boolean ascending,
                                                  final boolean nullsFirst) {
        return new Comparator() {
            @Override
            public int compare( Object o1, Object o2 ) {
                Object value1;
                Object value2;

                if ( ascending ) {
                    value1 =  ( o1 );
                    value2 =  ( o2 );
                } else {
                    value1 =  ( o2 );
                    value2 =  ( o1 );
                }

                return Sorting.compare(value1, value2, nullsFirst);
            }
        };
    }

public static Comparator thisUniversalComparator ( final boolean ascending , final boolean nullsFirst ) { return new Comparator ( ) { @ Override public int compare ( Object o1 , Object o2 ) { Object value1 ; Object value2 ; if ( ascending ) { value1 = ( o1 ) ; value2 = ( o2 ) ; } else { value1 = ( o2 ) ; value2 = ( o1 ) ; } return Sorting . compare ( value1 , value2 , nullsFirst ) ; } } ; }

This creates the universal comparator object used for "this".

@param ascending if this should be ascending or descending.
@return

This creates the universal comparator object used for this .


public VALUE get( KEY key ) {
        CacheEntry<KEY, VALUE> cacheEntry = map.get( key );
        if ( cacheEntry != null ) {
            cacheEntry.readCount.incrementAndGet();
            return cacheEntry.value;
        } else {
            return null;
        }

    }

public VALUE get ( KEY key ) { CacheEntry < KEY , VALUE > cacheEntry = map . get ( key ) ; if ( cacheEntry != null ) { cacheEntry . readCount . incrementAndGet ( ) ; return cacheEntry . value ; } else { return null ; } }

Get the value from the cache. It does not touch the lock so
reads are fast.
This does not touch the order list so it is fast.
@param key the key
@return value

Get the value from the cache . It does not touch the lock so reads are fast . This does not touch the order list so it is fast .


public VALUE getSilent( KEY key ) {
        CacheEntry<KEY, VALUE> cacheEntry = map.get( key );
        if ( cacheEntry != null ) {
            return cacheEntry.value;
        } else {
            return null;
        }

    }

public VALUE getSilent ( KEY key ) { CacheEntry < KEY , VALUE > cacheEntry = map . get ( key ) ; if ( cacheEntry != null ) { return cacheEntry . value ; } else { return null ; } }

Used for testing as it gets the value without updating stats
@param key key
@return value


private final int order() {
        int order = count.incrementAndGet();
        if ( order > Integer.MAX_VALUE - 100 ) {
            count.set( 0 );
        }
        return order;
    }

private final int order ( ) { int order = count . incrementAndGet ( ) ; if ( order > Integer . MAX_VALUE - 100 ) { count . set ( 0 ) ; } return order ; }


private final void evictIfNeeded() {
        if ( list.size() > evictSize ) {

            final List<CacheEntry<KEY, VALUE>> killList = list.sortAndReturnPurgeList( 0.1f );

            for ( CacheEntry<KEY, VALUE> cacheEntry : killList ) {
                map.remove( cacheEntry.key );
            }
        }

    }

private final void evictIfNeeded ( ) { if ( list . size ( ) > evictSize ) { final List < CacheEntry < KEY , VALUE > > killList = list . sortAndReturnPurgeList ( 0.1f ) ; for ( CacheEntry < KEY , VALUE > cacheEntry : killList ) { map . remove ( cacheEntry . key ) ; } } }


private void dynamicallyInitIfNeeded( Object value ) {
        /* Check to see if this class was already initialized,
         * if not, initialize it based on the type of the value.
		 */
        if ( !isInitialized() ) {
            if ( value instanceof Integer ) {
                init( new Integer( min.intValue() ), new Integer( max.intValue() ) );
            } else if ( value instanceof Byte ) {
                init( new Byte( min.byteValue() ), new Byte( max.byteValue() ) );
            } else if ( value instanceof Short ) {
                init( new Short( min.shortValue() ), new Short( max.shortValue() ) );
            } else {
                init( min, max );
            }
        }
    }

If the type was not initialized, we can still figure it out at runtime.

@param value

If the type was not initialized we can still figure it out at runtime .


public final ByteBuffer allocateBuffer(int size) {

        if (RECYCLE_BUFFER) {
            ByteBuffer spentBuffer = recycleChannel.poll();
            if (spentBuffer == null) {
                spentBuffer = ByteBuffer.allocateDirect(size);
            }
            spentBuffer.clear();
            return spentBuffer;
        } else {
            return ByteBuffer.allocateDirect(size);
        }
    }

public final ByteBuffer allocateBuffer ( int size ) { if ( RECYCLE_BUFFER ) { ByteBuffer spentBuffer = recycleChannel . poll ( ) ; if ( spentBuffer == null ) { spentBuffer = ByteBuffer . allocateDirect ( size ) ; } spentBuffer . clear ( ) ; return spentBuffer ; } else { return ByteBuffer . allocateDirect ( size ) ; } }

This gets called from the http post handler  or event bus handler.

@param size the size of the buffer that you would like.
@return

This gets called from the http post handler or event bus handler .


private boolean determineIfWeShouldExit() {
        boolean shouldStop = stop.get();
        if (!shouldStop) {
            Thread.interrupted();
        } else {
            System.out.println("Exiting processing loop as requested");
            return true;
        }
        return false;
    }

private
boolean
determineIfWeShouldExit
(
)
{
boolean
shouldStop
=
stop
.
get
(
)
;
if
(
!
shouldStop
)
{
Thread
.
interrupted
(
)
;
}
else
{
System
.
out
.
println
(
"Exiting processing loop as requested"
)
;
return
true
;
}
return
false
;
}

See if it is time to stop
We have been interrupted. Should we ignore it or break out of the loop.

@return

See if it is time to stop We have been interrupted . Should we ignore it or break out of the loop .


private void manageInputWriterChannel() throws InterruptedException {

        try {

            ByteBuffer dataToWriteToFile;
            dataToWriteToFile = inputChannel.poll();  //no wait

            //If it is null, it means the inputChannel is empty and we need to flush.
            if (dataToWriteToFile == null) {
                queueEmptyMaybeFlush();
                dataToWriteToFile = inputChannel.poll();
            }

            //If it is still null, this means that we need to wait
            //for more items to show up in the inputChannel.
            if (dataToWriteToFile == null) {
                dataToWriteToFile = waitForNextDataToWrite();
            }

            //We have to check for null again because we could have been interrupted.
            if (dataToWriteToFile != null) {
                //Write it
                writer.nextBufferToWrite(dataToWriteToFile);
                //Then give it back
                if (RECYCLE_BUFFER) {
                    recycleChannel.offer(dataToWriteToFile);
                }

            }

        } catch (InterruptedException ex) {
            throw ex;
        } catch (Exception ex) {
            ex.printStackTrace();
            ex.printStackTrace(System.err);
        }

    }

Queue and batch writer main logic.
This is where the magic happens.

@throws InterruptedException

Queue and batch writer main logic . This is where the magic happens .