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

functionalj.lens.lenses.StreamPlusAccess Maven / Gradle / Ivy

There is a newer version: 1.0.17
Show newest version
// ============================================================================
// Copyright (c) 2017-2021 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.lenses;

import java.util.List;
import java.util.function.Function;

import functionalj.lens.core.AccessParameterized;
import functionalj.lens.core.AccessUtils;
import functionalj.stream.StreamPlus;
import lombok.val;


@FunctionalInterface
public interface StreamPlusAccess> 
        extends ObjectAccess>, AccessParameterized, TYPE, AnyAccess> {
    
    public static > StreamPlusAccess of(Function> read, Function, A> createAccess) {
        val accessParameterized = new AccessParameterized, T, A>() {
            @Override
            public StreamPlus applyUnsafe(H host) throws Exception {
                return read.apply(host);
            }
            @Override
            public A createSubAccessFromHost(Function accessToParameter) {
                return createAccess.apply(accessToParameter);
            }
        };
        return AccessUtils.createSubStreamPlusAccess(accessParameterized, read);
    }
    
    
    public AccessParameterized, TYPE, TYPEACCESS> accessParameterized();
    
    @Override
    public default StreamPlus applyUnsafe(HOST host) throws Exception {
        return accessParameterized().applyUnsafe(host);
    }
    
    @Override
    public default TYPEACCESS createSubAccessFromHost(Function accessToSub) {
        return accessParameterized().createSubAccessFromHost(accessToSub);
    }
    
    public default ListAccess toList() {
        val spec        = accessParameterized();
        val specWithSub = new AccessParameterized, TYPE, TYPEACCESS>() {
            @Override
            public List applyUnsafe(HOST host) throws Exception{
                val streamPlus = spec.apply(host);
                return streamPlus.toList();
            }
            @Override
            public TYPEACCESS createSubAccessFromHost(Function accessToParameter) {
                return spec.createSubAccessFromHost(accessToParameter);
            }
        };
        return () -> specWithSub;
    }
    
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy