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

eu.lunisolar.magma.func.IA Maven / Gradle / Ivy

There is a newer version: 3.0.0
Show newest version
/*
 * This file is part of "lunisolar-magma".
 *
 * (C) Copyright 2014-2019 Lunisolar (http://lunisolar.eu/).
 *
 * 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 eu.lunisolar.magma.func;

import eu.lunisolar.magma.basics.meta.aType;
import eu.lunisolar.magma.basics.meta.aType.*;
import eu.lunisolar.magma.basics.meta.functional.IndexedRead;
import eu.lunisolar.magma.basics.meta.functional.IndexedWrite;
import eu.lunisolar.magma.basics.meta.functional.type.OiFunction;
import eu.lunisolar.magma.basics.meta.functional.type.TieConsumer;
import eu.lunisolar.magma.basics.meta.functional.type.TieFunction;
import eu.lunisolar.magma.func.function.to.LToIntFunction;

import java.lang.reflect.*;
import java.util.*;

import static eu.lunisolar.magma.func.consumer.primitives.obj.LTieBoolConsumer.tieBoolCons;
import static eu.lunisolar.magma.func.consumer.primitives.obj.LTieByteConsumer.tieByteCons;
import static eu.lunisolar.magma.func.consumer.primitives.obj.LTieCharConsumer.tieCharCons;
import static eu.lunisolar.magma.func.consumer.primitives.obj.LTieConsumer.tieCons;
import static eu.lunisolar.magma.func.consumer.primitives.obj.LTieDblConsumer.tieDblCons;
import static eu.lunisolar.magma.func.consumer.primitives.obj.LTieFltConsumer.tieFltCons;
import static eu.lunisolar.magma.func.consumer.primitives.obj.LTieIntConsumer.tieIntCons;
import static eu.lunisolar.magma.func.consumer.primitives.obj.LTieLongConsumer.tieLongCons;
import static eu.lunisolar.magma.func.consumer.primitives.obj.LTieSrtConsumer.tieSrtCons;
import static eu.lunisolar.magma.func.function.from.LOiFunction.oiFunc;
import static eu.lunisolar.magma.func.function.to.LOiToByteFunction.oiToByteFunc;
import static eu.lunisolar.magma.func.function.to.LOiToCharFunction.oiToCharFunc;
import static eu.lunisolar.magma.func.function.to.LOiToDblFunction.oiToDblFunc;
import static eu.lunisolar.magma.func.function.to.LOiToFltFunction.oiToFltFunc;
import static eu.lunisolar.magma.func.function.to.LOiToIntFunction.oiToIntFunc;
import static eu.lunisolar.magma.func.function.to.LOiToLongFunction.oiToLongFunc;
import static eu.lunisolar.magma.func.function.to.LOiToSrtFunction.oiToSrtFunc;
import static eu.lunisolar.magma.func.predicate.LObjIntPredicate.objIntPred;

/**
 * Aggregates access functions (with index) to a specific container (e.g. array, list) of specific type (e.g. int[])
 * Such access do not protect against concurrent modification.     
 */
public interface IA extends IndexedRead, IndexedWrite {

	public static class The implements IA {

		private static final IA> LIST = iA(List::size, oiFunc(List::get), tieCons(List::set));

		private static final IA INT_ARRAY = iA(IA::length, oiToIntFunc(IA::getInt), tieIntCons(IA::setInt));
		private static final IA LONG_ARRAY = iA(IA::length, oiToLongFunc(IA::getLong), tieLongCons(IA::setLong));
		private static final IA DOUBLE_ARRAY = iA(IA::length, oiToDblFunc(IA::getDouble), tieDblCons(IA::setDouble));
		private static final IA BYTE_ARRAY = iA(IA::length, oiToByteFunc(IA::getByte), tieByteCons(IA::setByte));
		private static final IA SHORT_ARRAY = iA(IA::length, oiToSrtFunc(IA::getShort), tieSrtCons(IA::setShort));
		private static final IA CHAR_ARRAY = iA(IA::length, oiToCharFunc(IA::getChar), tieCharCons(IA::setChar));
		private static final IA FLOAT_ARRAY = iA(IA::length, oiToFltFunc(IA::getFloat), tieFltCons(IA::setFloat));
		private static final IA BOOL_ARRAY = iA(IA::length, objIntPred(IA::getBoolean), tieBoolCons(IA::setBoolean));
		private static final IA> ARRAY = iA(a -> a.length, oiFunc((a, i) -> a[i]), tieCons((a, i, e) -> a[i] = e));

		private final LToIntFunction sizeFunc;

		private final OiFunction getter;
		private final TieConsumer setter;
		private final TieFunction setFunc;

		public The(LToIntFunction size, OiFunction getter, TieConsumer setter) {
			this.sizeFunc = size;
			this.getter = getter;
			this.setter = setter;
			this.setFunc = setter.toTieFunction();
		}

		public LToIntFunction genericSizeFunc() {
			return sizeFunc;
		}

		public LToIntFunction sizeFunc() {
			return genericSizeFunc();
		}

		public OiFunction genericGetter() {
			return getter;
		}

		public TieConsumer genericSetter() {
			return setter;
		}

		public TieFunction genericSetFunc() {
			return setFunc;
		}

	}

	public static  IA iA(LToIntFunction size, OiFunction oi, TieConsumer tie) {
		return new IA.The<>(size, oi, tie);
	}

	public static , A extends a> IA list() {
		return (IA) The.LIST;
	}

	public static IA intArray() {
		return The.INT_ARRAY;
	}

	public static IA longArray() {
		return The.LONG_ARRAY;
	}

	public static IA doubleArray() {
		return The.DOUBLE_ARRAY;
	}

	public static IA byteArray() {
		return The.BYTE_ARRAY;
	}

	public static IA shortArray() {
		return The.SHORT_ARRAY;
	}

	public static IA charArray() {
		return The.CHAR_ARRAY;
	}

	public static IA floatArray() {
		return The.FLOAT_ARRAY;
	}

	public static IA boolArray() {
		return The.BOOL_ARRAY;
	}

	public static > IA array() {
		return (IA) The.ARRAY;
	}

	// 

	// boolean

	public static int length(boolean[] array) {
		return array.length;
	}

	public static void setBoolean(boolean[] array, int index, boolean element) {
		array[index] = element;
	}

	public static boolean getBoolean(boolean[] array, int index) {
		return array[index];
	}

	// byte

	public static int length(byte[] array) {
		return array.length;
	}

	public static void setByte(byte[] array, int index, byte element) {
		array[index] = element;
	}

	public static byte getByte(byte[] array, int index) {
		return array[index];
	}

	// double

	public static int length(double[] array) {
		return array.length;
	}

	public static void setDouble(double[] array, int index, double element) {
		array[index] = element;
	}

	public static double getDouble(double[] array, int index) {
		return array[index];
	}

	// char

	public static int length(char[] array) {
		return array.length;
	}

	public static void setChar(char[] array, int index, char element) {
		array[index] = element;
	}

	public static char getChar(char[] array, int index) {
		return array[index];
	}

	// short

	public static int length(short[] array) {
		return array.length;
	}

	public static void setShort(short[] array, int index, short element) {
		array[index] = element;
	}

	public static short getShort(short[] array, int index) {
		return array[index];
	}

	// float

	public static int length(float[] array) {
		return array.length;
	}

	public static void setFloat(float[] array, int index, float element) {
		array[index] = element;
	}

	public static float getFloat(float[] array, int index) {
		return array[index];
	}

	// int

	public static int length(int[] array) {
		return array.length;
	}

	public static void setInt(int[] array, int index, int element) {
		array[index] = element;
	}

	public static int getInt(int[] array, int index) {
		return array[index];
	}

	// long

	public static int length(long[] array) {
		return array.length;
	}

	public static void setLong(long[] array, int index, long element) {
		array[index] = element;
	}

	public static long getLong(long[] array, int index) {
		return array[index];
	}

	// T

	public static  int length(T[] array) {
		return array.length;
	}

	public static  void set(T[] array, int index, T element) {
		array[index] = element;
	}

	public static  T get(T[] array, int index) {
		return array[index];
	}

	// 

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy