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 static functionalj.lens.core.LensSpec.selfRead;
import static functionalj.lens.core.LensSpec.selfWrite;

import java.util.List;

import functionalj.lens.core.LensSpec;
import functionalj.lens.core.LensSpecParameterized;
import functionalj.lens.core.LensSpecParameterized2;
import functionalj.lens.core.LensType;
import functionalj.lens.core.LensUtils;
import functionalj.lens.lenses.AnyAccess;
import functionalj.lens.lenses.AnyLens;
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.ListLens;
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 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 BooleanAccess True  = any -> true;
    public static final BooleanAccess False = any -> false;
    
    public static final TheListLens   theList   = new TheListLens();
    public static final TheTuple2Lens theTuple2 = new 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);
    }
    
    
    
    
    //== Internal use only ==
    
    public static class TheListLens implements ListLens, Object, ObjectLens, Object>> {
        
        private static final LensSpecParameterized, List, Object, ObjectLens, Object>> 
                common = LensUtils.createLensSpecParameterized(selfRead(), selfWrite(), ObjectLens::of);
        
        public , T>, SL extends AnyLens, T>> 
                ListLens, T, SL> of(LensType, T, SA, SL> type) {
            LensSpecParameterized, List, T, SL> spec
                    = LensUtils.createLensSpecParameterized(LensSpec.selfRead(), LensSpec.selfWrite(), s -> type.newLens(s));
            ListLens, T, SL> listLens = ListLens.of(spec);
            return listLens;
        }
        
        @SuppressWarnings({ "rawtypes", "unchecked" })
        @Override
        public LensSpecParameterized, List, Object, ObjectLens, Object>> lensSpecParameterized() {
            return (LensSpecParameterized)common;
        }
    }
    
    //-- Tuple2 --
    
    public static class TheTuple2Lens implements Tuple2Lens, Object, Object, 
            ObjectLens, Object>, ObjectLens, Object>> {
        
        // I am tired .... just goes with this.
        private static final LensSpecParameterized2, Tuple2, Object, Object, ObjectLens, Object>, ObjectLens, Object>> 
                common = new LensSpecParameterized2, Tuple2, Object, Object, ObjectLens,Object>, ObjectLens,Object>>() {
                    @Override
                    public LensSpec, Tuple2> getSpec() {
                        return LensSpec.of(selfRead(), selfWrite());
                    }
                    @Override
                    public ObjectLens, Object> createSubLens1(
                            LensSpec, Object> subSpec) {
                        return ObjectLens.of(subSpec);
                    }
                    @Override
                    public ObjectLens, Object> createSubLens2(
                            LensSpec, Object> subSpec) {
                        return ObjectLens.of(subSpec);
                    }
                };
        
        public , T1>, T2ACCESS extends AnyAccess, T2>, 
                       T1LENS   extends AnyLens, T1>,   T2LENS   extends AnyLens, T2>>
                Tuple2Lens, T1, T2, T1LENS, T2LENS> of(
                        LensType, T1, T1ACCESS, T1LENS> t1Type,
                        LensType, T2, T2ACCESS, T2LENS> t2Type) {
            return Tuple2Lens.of(selfRead(), selfWrite(), t1Type, t2Type);
        }
        
        @SuppressWarnings({ "rawtypes", "unchecked" })
        @Override
        public LensSpecParameterized2, Tuple2, Object, Object, 
            ObjectLens, Object>, ObjectLens, Object>> lensSpecParameterized2() {
            return (LensSpecParameterized2)common;
        }
        
    }
    
}