Class Lens<S,A>
java.lang.Object
cloud.opencode.base.functional.optics.Lens<S,A>
- Type Parameters:
S- source/whole type - 源/整体类型A- target/part type - 目标/部分类型
Lens - Functional lens for immutable data access and modification
Lens - 用于不可变数据访问和修改的函数式透镜
A lens provides a way to focus on a part of a data structure, allowing both reading and functional updating of that part. Particularly useful for nested immutable data structures like records.
透镜提供了一种聚焦数据结构某部分的方式,允许读取和函数式更新该部分。 对于嵌套的不可变数据结构(如 record)特别有用。
Features | 主要功能:
- Get: read a part of a structure - 获取: 读取结构的一部分
- Set: create a copy with modified part - 设置: 创建带修改部分的副本
- Modify: transform a part - 修改: 转换一部分
- Compose: combine lenses - 组合: 组合透镜
Usage Examples | 使用示例:
// Define a lens for Person.name
Lens<Person, String> nameLens = Lens.of(
Person::name,
(person, name) -> new Person(name, person.age())
);
// Read value
String name = nameLens.get(person);
// Create modified copy
Person updated = nameLens.set(person, "Alice");
// Transform value
Person upperCased = nameLens.modify(person, String::toUpperCase);
// Compose lenses for nested access
Lens<Company, String> ceoNameLens = companyToPersonLens.andThen(nameLens);
// Record lens example
record Address(String city, String street) {}
record Person(String name, Address address) {}
Lens<Person, Address> addressLens = Lens.of(
Person::address,
(p, addr) -> new Person(p.name(), addr)
);
Lens<Address, String> cityLens = Lens.of(
Address::city,
(addr, city) -> new Address(city, addr.street())
);
// Compose to access nested field
Lens<Person, String> personCityLens = addressLens.andThen(cityLens);
Person moved = personCityLens.set(person, "New York");
Laws | 透镜定律:
- Get-Set: set(s, get(s)) == s - 获取-设置: set(s, get(s)) == s
- Set-Get: get(set(s, a)) == a - 设置-获取: get(set(s, a)) == a
- Set-Set: set(set(s, a), b) == set(s, b) - 设置-设置: set(set(s, a), b) == set(s, b)
Performance | 性能特性:
- Get: O(1) - 获取: O(1)
- Set: O(1) (assuming O(1) copy) - 设置: O(1) (假设 O(1) 复制)
- Compose: O(depth) for nested access - 组合: O(深度) 用于嵌套访问
Security | 安全性:
- Thread-safe: Yes (immutable) - 线程安全: 是 (不可变)
- Null-safe: Depends on getter/setter - 空值安全: 取决于 getter/setter
- Since:
- JDK 25, opencode-base-functional V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionCompose with another lens (this lens first, then other) 与另一个透镜组合(先应用此透镜,然后应用其他)Convert to an OptionalLens 转换为 OptionalLensCompose with another lens (other lens first, then this) 与另一个透镜组合(先应用其他,然后应用此透镜)static <S,A> Lens <S, A> forRecord(Function<S, A> getter, BiFunction<S, A, S> constructor) Create a lens for a record component 为 record 组件创建透镜Get the focused value from the source 从源获取聚焦的值getter()Get the getter function 获取 getter 函数static <S> Lens<S, S> identity()Create an identity lens 创建恒等透镜modifier(UnaryOperator<A> modifier) Create a modifier function for this lens 为此透镜创建修改器函数modify(S source, UnaryOperator<A> modifier) Modify the focused value using a function 使用函数修改聚焦的值static <S,A> Lens <S, A> of(Function<S, A> getter, BiFunction<S, A, S> setter) Create a lens from getter and setter 从 getter 和 setter 创建透镜Set the focused value, returning a new source 设置聚焦的值,返回新的源BiFunction<S, A, S> setter()Get the setter function 获取 setter 函数toString()
-
Method Details
-
of
Create a lens from getter and setter 从 getter 和 setter 创建透镜- Type Parameters:
S- source/whole type - 源/整体类型A- target/part type - 目标/部分类型- Parameters:
getter- function to get the part - 获取部分的函数setter- function to set the part (returns new whole) - 设置部分的函数(返回新整体)- Returns:
- lens
-
identity
Create an identity lens 创建恒等透镜- Type Parameters:
S- type - 类型- Returns:
- identity lens
-
get
-
set
-
modify
Modify the focused value using a function 使用函数修改聚焦的值- Parameters:
source- the source structure - 源结构modifier- function to modify the value - 修改值的函数- Returns:
- new source with modified part
-
modifier
Create a modifier function for this lens 为此透镜创建修改器函数- Parameters:
modifier- function to modify the value - 修改值的函数- Returns:
- function that modifies source
-
andThen
Compose with another lens (this lens first, then other) 与另一个透镜组合(先应用此透镜,然后应用其他)Creates a lens that focuses on a part of the part.
创建聚焦于部分的部分的透镜。
- Type Parameters:
B- deeper target type - 更深的目标类型- Parameters:
other- lens to compose with - 要组合的透镜- Returns:
- composed lens
-
compose
-
asOptional
Convert to an OptionalLens 转换为 OptionalLens- Returns:
- optional lens
-
getter
-
setter
-
forRecord
Create a lens for a record component 为 record 组件创建透镜Helper for creating lenses for record fields.
为 record 字段创建透镜的辅助方法。
- Type Parameters:
S- record type - record 类型A- component type - 组件类型- Parameters:
getter- record component accessor - record 组件访问器constructor- record constructor with updated field - 带更新字段的 record 构造器- Returns:
- lens for record component
-
toString
-