com.bmd.android.collection.translator.Translators Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of robo-fashion Show documentation
Show all versions of robo-fashion Show documentation
Easily iterate through Android sparse collections
/**
* 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