com.github.dm.rf.android.translator.Translators Maven / Gradle / Ivy
/**
* 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.github.dm.rf.android.translator;
import com.github.dm.rf.android.entry.IntSparseEntry;
import com.github.dm.rf.android.entry.LongSparseEntry;
import com.github.dm.rf.android.entry.ObjectSparseEntry;
import com.github.dm.rf.android.entry.SparseBooleanArrayEntry;
import com.github.dm.rf.android.entry.SparseIntArrayEntry;
import com.github.dm.rf.android.entry.SparseLongArrayEntry;
import com.github.dm.rf.android.entry.SparseObjectEntry;
/**
* Utility class providing helper methods for creating and managing {@link Translator} objects.
*
* Created by davide-maestroni 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 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 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