functionalj.lens.core.AccessUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of functionalj-core Show documentation
Show all versions of functionalj-core Show documentation
The module for FunctionalJ Core.
// ============================================================================
// 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.core;
import java.util.List;
import java.util.Optional;
import java.util.function.Function;
import functionalj.lens.lenses.AnyAccess;
import functionalj.lens.lenses.FuncListAccess;
import functionalj.lens.lenses.ListAccess;
import functionalj.lens.lenses.NullableAccess;
import functionalj.lens.lenses.OptionalAccess;
import functionalj.lens.lenses.ResultAccess;
import functionalj.list.FuncList;
import functionalj.result.Result;
import lombok.val;
import nullablej.nullable.Nullable;
@SuppressWarnings("javadoc")
public class AccessUtils {
// Nullable
public static > NullableAccess
createSubNullableAccess(
AccessParameterized, TYPE, TYPEACCESS> accessParameterized,
Function> read) {
val specWithSub = new AccessParameterized, TYPE, TYPEACCESS>() {
@Override
public Nullable applyUnsafe(HOST host) throws Exception {
return read.apply(host);
}
@Override
public TYPEACCESS createSubAccessFromHost(Function accessToParameter) {
return accessParameterized.createSubAccessFromHost(accessToParameter);
}
};
return () -> specWithSub;
}
// Optional
public static > OptionalAccess
createSubOptionalAccess(
AccessParameterized, TYPE, TYPEACCESS> accessParameterized,
Function> read) {
val specWithSub = new AccessParameterized, TYPE, TYPEACCESS>() {
@Override
public Optional applyUnsafe(HOST host) throws Exception {
return read.apply(host);
}
@Override
public TYPEACCESS createSubAccessFromHost(Function accessToParameter) {
return accessParameterized.createSubAccessFromHost(accessToParameter);
}
};
return () -> specWithSub;
}
// Result
public static > ResultAccess
createSubResultAccess(
AccessParameterized, TYPE, TYPEACCESS> accessParameterized,
Function> read) {
val specWithSub = new AccessParameterized, TYPE, TYPEACCESS>() {
@Override
public Result applyUnsafe(HOST host) throws Exception {
return read.apply(host);
}
@Override
public TYPEACCESS createSubAccessFromHost(Function accessToParameter) {
return accessParameterized.createSubAccessFromHost(accessToParameter);
}
};
return () -> specWithSub;
}
// List
public static > ListAccess
createSubListAccess(
AccessParameterized, TYPE, TYPEACCESS> accessParameterized,
Function> read) {
val specWithSub = new AccessParameterized, TYPE, TYPEACCESS>() {
@Override
public List applyUnsafe(HOST host) throws Exception {
return read.apply(host);
}
@Override
public TYPEACCESS createSubAccessFromHost(Function accessToParameter) {
return accessParameterized.createSubAccessFromHost(accessToParameter);
}
};
return () -> specWithSub;
}
public static > FuncListAccess
createSubFuncListAccess(
AccessParameterized, TYPE, TYPEACCESS> accessParameterized,
Function> read) {
val specWithSub = new AccessParameterized, TYPE, TYPEACCESS>() {
@Override
public FuncList applyUnsafe(HOST host) throws Exception {
return read.apply(host);
}
@Override
public TYPEACCESS createSubAccessFromHost(Function accessToParameter) {
return accessParameterized.createSubAccessFromHost(accessToParameter);
}
};
return () -> specWithSub;
}
public static >
NullableAccess createNullableAccess(
Function> accessNullable,
Function, TYPELENS> createSubLens) {
val accessWithSub = new AccessParameterized, TYPE, TYPELENS>() {
@Override
public Nullable applyUnsafe(HOST host) throws Exception {
return accessNullable.apply(host);
}
@Override
public TYPELENS createSubAccessFromHost(Function accessToParameter) {
return createSubLens.apply(accessToParameter);
}
};
return () -> accessWithSub;
}
public static >
ResultAccess createResultAccess(
Function> accessResult,
Function, TYPELENS> createSubLens) {
val accessWithSub = new AccessParameterized, TYPE, TYPELENS>() {
@Override
public Result applyUnsafe(HOST host) throws Exception {
return accessResult.apply(host);
}
@Override
public TYPELENS createSubAccessFromHost(Function accessToParameter) {
return createSubLens.apply(accessToParameter);
}
};
return () -> accessWithSub;
}
}