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

functionalj.lens.Access Maven / Gradle / Ivy

There is a newer version: 1.0.17
Show newest version
// ============================================================================
// Copyright (c) 2017-2019 Nawapunth Manusitthipol (NawaMan - http://nawaman.net).
// ----------------------------------------------------------------------------
// MIT License
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
// ============================================================================
package functionalj.lens;

import java.math.BigDecimal;
import java.math.BigInteger;

import functionalj.lens.core.LensSpec;
import functionalj.lens.core.LensType;
import functionalj.lens.lenses.AnyAccess;
import functionalj.lens.lenses.AnyLens;
import functionalj.lens.lenses.BigDecimalLens;
import functionalj.lens.lenses.BigIntegerLens;
import functionalj.lens.lenses.BooleanAccess;
import functionalj.lens.lenses.BooleanLens;
import functionalj.lens.lenses.ComparableLens;
import functionalj.lens.lenses.DoubleLens;
import functionalj.lens.lenses.IntegerLens;
import functionalj.lens.lenses.LongLens;
import functionalj.lens.lenses.ObjectLens;
import functionalj.lens.lenses.StringLens;
import functionalj.lens.lenses.Tuple2Lens;
import functionalj.tuple.Tuple2;

@SuppressWarnings("javadoc")
public interface Access {
    
    public static final AnyLens theObject  = AnyLens    .of(LensSpec.of(Object.class));
    public static final BooleanLens    theBoolean = BooleanLens.of(LensSpec.of(Boolean.class));
    public static final StringLens      theString  = StringLens .of(LensSpec.of(String.class));
    public static final IntegerLens    theInteger = IntegerLens.of(LensSpec.of(Integer.class));
    public static final LongLens          theLong    = LongLens   .of(LensSpec.of(Long.class));
    public static final DoubleLens      theDouble  = DoubleLens .of(LensSpec.of(Double.class));
    
    public static final BigIntegerLens theBigInteger = BigIntegerLens.of(LensSpec.of(BigInteger.class));
    public static final BigDecimalLens theBigDecimal = BigDecimalLens.of(LensSpec.of(BigDecimal.class));
    
    public static final AnyLens    $O = theObject;
    public static final BooleanLens       $B = theBoolean;
    public static final StringLens         $S = theString;
    public static final IntegerLens       $I = theInteger;
    public static final LongLens             $L = theLong;
    public static final DoubleLens         $D = theDouble;
    public static final BigIntegerLens $BI = theBigInteger;
    public static final BigDecimalLens $BD = theBigDecimal;
    
    public static final BooleanAccess True  = any -> true;
    public static final BooleanAccess False = any -> false;
    
    public static final Accesses.TheListLens   theList   = new Accesses.TheListLens();
    public static final Accesses.TheTuple2Lens theTuple2 = new Accesses.TheTuple2Lens();
    
    public static  AnyLens theItem() {
        return AnyLens.of(LensSpec.of((T item) -> item, (T host, T newItem) -> newItem));
    }
    public static  ObjectLens theObject() {
        return ObjectLens.of(LensSpec.of((T item) -> item, (T host, T newItem) -> newItem));
    }
    public static > ComparableLens theComparable() {
        return ComparableLens.of(LensSpec.of((T item) -> item, (T host, T newItem) -> newItem));
    }
    public static , T1>, T2ACCESS extends AnyAccess, T2>, 
            T1LENS   extends AnyLens, T1>,   T2LENS   extends AnyLens, T2>>
        Tuple2Lens, T1, T2, T1LENS, T2LENS> theTupleOf(
             LensType, T1, T1ACCESS, T1LENS> t1Type,
             LensType, T2, T2ACCESS, T2LENS> t2Type) {
        return theTuple2.of(t1Type, t2Type);
    }
    
    //-- Each --
    
    public static final AnyLens eachObject  = theObject;
    public static final BooleanLens    eachBoolean = theBoolean;
    public static final StringLens      eachString  = theString;
    public static final IntegerLens    eachInteger = theInteger;
    public static final LongLens          eachLong    = theLong;
    public static final DoubleLens      eachDouble  = theDouble;
    
    public static final BigIntegerLens eachBigInteger = theBigInteger;
    public static final BigDecimalLens eachBigDecimal = theBigDecimal;
    
    public static final Accesses.TheListLens   eachList   = theList;
    public static final Accesses.TheTuple2Lens eachTuple2 = theTuple2;
    
    public static  AnyLens    eachItem()   { return theItem(); }
    public static  ObjectLens eachObject() { return theObject(); }
    
    public static > ComparableLens eachComparable() {
        return theComparable();
    }
    public static , T1>, T2ACCESS extends AnyAccess, T2>, 
            T1LENS   extends AnyLens, T1>,   T2LENS   extends AnyLens, T2>>
        Tuple2Lens, T1, T2, T1LENS, T2LENS> eachTupleOf(
             LensType, T1, T1ACCESS, T1LENS> t1Type,
             LensType, T2, T2ACCESS, T2LENS> t2Type) {
        return theTupleOf(t1Type, t2Type);
    }
}