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

functionalj.lens.Accesses Maven / Gradle / Ivy

There is a newer version: 1.0.17
Show newest version
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.ListLens;
import functionalj.lens.lenses.ObjectLens;
import functionalj.lens.lenses.Tuple2Lens;
import functionalj.tuple.Tuple2;

public class Accesses {
    
    //== 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;
        }
        
    }
    
}