All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.bmd.android.collection.translator.Translators Maven / Gradle / Ivy

There is a newer version: 3.0.0
Show newest version
/**
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.bmd.android.collection.translator;

import com.bmd.android.collection.entry.IntSparseEntry;
import com.bmd.android.collection.entry.LongSparseEntry;
import com.bmd.android.collection.entry.ObjectSparseEntry;
import com.bmd.android.collection.entry.SparseBooleanArrayEntry;
import com.bmd.android.collection.entry.SparseIntArrayEntry;
import com.bmd.android.collection.entry.SparseLongArrayEntry;
import com.bmd.android.collection.entry.SparseObjectEntry;

/**
 * Utility class providing helper methods for creating and managing {@link Translator} objects.
 * 

* Created by davide on 3/15/14. */ public class Translators { private static volatile Translator sBooleanValueObjectTranslator; private static volatile ToBooleanTranslator sBooleanValueTranslator; private static volatile Translator sElementObjectTranslator; private static volatile FullBooleanTranslator sIdentityBooleanTranslator; private static volatile FullIntTranslator sIdentityIntTranslator; private static volatile FullLongTranslator sIdentityLongTranslator; private static volatile FullTranslator sIdentityTranslator; private static volatile Translator sIntKeyObjectTranslator; private static volatile ToIntTranslator sIntKeyTranslator; private static volatile Translator sIntValueObjectTranslator; private static volatile ToIntTranslator sIntValueTranslator; private static volatile Translator sLongKeyObjectTranslator; private static volatile ToLongTranslator sLongKeyTranslator; private static volatile Translator sLongValueObjectTranslator; private static volatile ToLongTranslator sLongValueTranslator; private static volatile Translator, ?> sObjectKeyTranslator; private static volatile Translator, ?> sObjectValueTranslator; /** * Avoid direct instantiation. */ private Translators() { } /** * Returns a {@link FullBooleanTranslator} instance that pass the specified values through the * translation unchanged. * * @return the translator. */ public static FullBooleanTranslator booleanIdentity() { if (sIdentityBooleanTranslator == null) { sIdentityBooleanTranslator = new FullBooleanTranslator() { @Override public boolean revert(final boolean value) { return value; } @Override public boolean translate(final boolean value) { return value; } }; } return sIdentityBooleanTranslator; } /** * Returns a translator which extracts the boolean value from * {@link com.bmd.android.collection.entry.SparseBooleanArrayEntry} elements. * * @param the element type. * @return the translator. */ public static ToBooleanTranslator booleanValue() { if (sBooleanValueTranslator == null) { sBooleanValueTranslator = new ToBooleanTranslator() { @Override public boolean translate(final SparseBooleanArrayEntry element) { return element.getValue(); } }; } //noinspection unchecked return (ToBooleanTranslator) sBooleanValueTranslator; } /** * Returns a translator which extracts the boolean value as an object from * {@link com.bmd.android.collection.entry.SparseBooleanArrayEntry} elements. * * @param the element type. * @return the translator. */ public static Translator booleanValueObject() { if (sBooleanValueObjectTranslator == null) { sBooleanValueObjectTranslator = new Translator() { @Override public Object translate(final SparseBooleanArrayEntry element) { return element.getValue(); } }; } //noinspection unchecked return (Translator) sBooleanValueObjectTranslator; } /** * Creates an {@link FullBooleanTranslator} wrapping the specified simple * {@link BooleanTranslator}. *

* Not that the returned translator will throw an exception if the reverse translation method * is called. * * @param translator the translator to wrap. * @return the full translator. */ public static FullBooleanTranslator full(final BooleanTranslator translator) { if (translator instanceof FullBooleanTranslator) { return (FullBooleanTranslator) translator; } return new FullBooleanTranslator() { @Override public boolean revert(final boolean value) { throw new UnsupportedOperationException(); } @Override public boolean translate(final boolean value) { return translator.translate(value); } }; } /** * Creates an {@link FullLongTranslator} wrapping the specified simple {@link LongTranslator}. *

* Not that the returned translator will throw an exception if the reverse translation method * is called. * * @param translator the translator to wrap. * @return the full translator. */ public static FullLongTranslator full(final LongTranslator translator) { if (translator instanceof FullLongTranslator) { return (FullLongTranslator) translator; } return new FullLongTranslator() { @Override public long revert(final long value) { throw new UnsupportedOperationException(); } @Override public long translate(final long value) { return translator.translate(value); } }; } /** * Creates an {@link FullIntTranslator} wrapping the specified simple {@link IntTranslator}. *

* Not that the returned translator will throw an exception if the reverse translation method * is called. * * @param translator the translator to wrap. * @return the full translator. */ public static FullIntTranslator full(final IntTranslator translator) { if (translator instanceof FullIntTranslator) { return (FullIntTranslator) translator; } return new FullIntTranslator() { @Override public int revert(final int value) { throw new UnsupportedOperationException(); } @Override public int translate(final int value) { return translator.translate(value); } }; } /** * Creates an {@link FullTranslator} wrapping the specified simple {@link Translator}. *

* Not that the returned translator will throw an exception if the reverse translation method * is called. * * @param translator the translator to wrap. * @param the input object type. * @param the output element type. * @return the full translator. */ public static FullTranslator full(final Translator translator) { if (translator instanceof FullTranslator) { return (FullTranslator) translator; } return new FullTranslator() { @Override public I revert(final O element) { throw new UnsupportedOperationException(); } @Override public O translate(final I element) { return translator.translate(element); } }; } /** * Returns a {@link FullTranslator} instance that pass the specified elements through the * translation unchanged. * * @param the element type. * @return the translator. */ public static FullTranslator identity() { if (sIdentityTranslator == null) { sIdentityTranslator = new FullTranslator() { @Override public Object revert(final Object element) { return element; } @Override public Object translate(final Object element) { return element; } }; } //noinspection unchecked return (FullTranslator) sIdentityTranslator; } /** * Returns a {@link FullIntTranslator} instance that pass the specified values through the * translation unchanged. * * @return the translator. */ public static FullIntTranslator intIdentity() { if (sIdentityIntTranslator == null) { sIdentityIntTranslator = new FullIntTranslator() { @Override public int revert(final int value) { return value; } @Override public int translate(final int value) { return value; } }; } return sIdentityIntTranslator; } /** * Returns a translator which extracts the int key from * {@link com.bmd.android.collection.entry.IntSparseEntry} elements. * * @param the element type. * @return the translator. */ public static ToIntTranslator intKey() { if (sIntKeyTranslator == null) { sIntKeyTranslator = new ToIntTranslator() { @Override public int translate(final IntSparseEntry element) { return element.getKey(); } }; } //noinspection unchecked return (ToIntTranslator) sIntKeyTranslator; } /** * Returns a translator which extracts the int key as an object from * {@link com.bmd.android.collection.entry.IntSparseEntry} elements. * * @param the element type. * @return the translator. */ public static Translator intKeyObject() { if (sIntKeyObjectTranslator == null) { sIntKeyObjectTranslator = new Translator() { @Override public Object translate(final IntSparseEntry element) { return element.getKey(); } }; } //noinspection unchecked return (Translator) sIntKeyObjectTranslator; } /** * Returns a translator which extracts the int value from * {@link com.bmd.android.collection.entry.SparseIntArrayEntry} elements. * * @param the element type. * @return the translator. */ public static ToIntTranslator intValue() { if (sIntValueTranslator == null) { sIntValueTranslator = new ToIntTranslator() { @Override public int translate(final SparseIntArrayEntry element) { return element.getValue(); } }; } //noinspection unchecked return (ToIntTranslator) sIntValueTranslator; } /** * Returns a translator which extracts the int value as an object from * {@link com.bmd.android.collection.entry.SparseIntArrayEntry} elements. * * @param the element type. * @return the translator. */ public static Translator intValueObject() { if (sIntValueObjectTranslator == null) { sIntValueObjectTranslator = new Translator() { @Override public Object translate(final SparseIntArrayEntry element) { return element.getValue(); } }; } //noinspection unchecked return (Translator) sIntValueObjectTranslator; } /** * Returns a {@link FullLongTranslator} instance that pass the specified values through the * translation unchanged. * * @return the translator. */ public static FullLongTranslator longIdentity() { if (sIdentityLongTranslator == null) { sIdentityLongTranslator = new FullLongTranslator() { @Override public long revert(final long value) { return value; } @Override public long translate(final long value) { return value; } }; } return sIdentityLongTranslator; } /** * Returns a translator which extracts the long key from * {@link com.bmd.android.collection.entry.LongSparseEntry} elements. * * @param the element type. * @return the translator. */ public static ToLongTranslator longKey() { if (sLongKeyTranslator == null) { sLongKeyTranslator = new ToLongTranslator() { @Override public long translate(final LongSparseEntry element) { return element.getKey(); } }; } //noinspection unchecked return (ToLongTranslator) sLongKeyTranslator; } /** * Returns a translator which extracts the long key as an object from * {@link com.bmd.android.collection.entry.LongSparseEntry} elements. * * @param the element type. * @return the translator. */ public static Translator longKeyObject() { if (sLongKeyObjectTranslator == null) { sLongKeyObjectTranslator = new Translator() { @Override public Object translate(final LongSparseEntry element) { return element.getKey(); } }; } //noinspection unchecked return (Translator) sLongKeyObjectTranslator; } /** * Returns a translator which extracts the long value from * {@link com.bmd.android.collection.entry.SparseLongArrayEntry} elements. * * @param the element type. * @return the translator. */ public static ToLongTranslator longValue() { if (sLongValueTranslator == null) { sLongValueTranslator = new ToLongTranslator() { @Override public long translate(final SparseLongArrayEntry element) { return element.getValue(); } }; } //noinspection unchecked return (ToLongTranslator) sLongValueTranslator; } /** * Returns a translator which extracts the long value as an object from * {@link com.bmd.android.collection.entry.SparseLongArrayEntry} elements. * * @param the element type. * @return the translator. */ public static Translator longValueObject() { if (sLongValueObjectTranslator == null) { sLongValueObjectTranslator = new Translator() { @Override public Object translate(final SparseLongArrayEntry element) { return element.getValue(); } }; } //noinspection unchecked return (Translator) sLongValueObjectTranslator; } /** * Returns a translator which returns elements as objects. * * @param the element type. * @return the translator. */ public static Translator object() { if (sElementObjectTranslator == null) { sElementObjectTranslator = new Translator, Object>() { @Override public Object translate(final ObjectSparseEntry element) { return element; } }; } //noinspection unchecked return (Translator) sElementObjectTranslator; } /** * Returns a translator which extracts the key as an object from * {@link com.bmd.android.collection.entry.ObjectSparseEntry} elements. * * @param the element type. * @return the translator. */ public static > Translator objectKey() { if (sObjectKeyTranslator == null) { sObjectKeyTranslator = new Translator, Object>() { @Override public Object translate(final ObjectSparseEntry element) { return element.getKey(); } }; } //noinspection unchecked return (Translator) sObjectKeyTranslator; } /** * Returns a translator which extracts the value as an object from * {@link com.bmd.android.collection.entry.SparseObjectEntry} elements. * * @param the element type. * @return the translator. */ public static > Translator objectValue() { if (sObjectValueTranslator == null) { sObjectValueTranslator = new Translator, Object>() { @Override public Object translate(final SparseObjectEntry element) { return element.getValue(); } }; } //noinspection unchecked return (Translator) sObjectValueTranslator; } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy