Skip to content

Commit

Permalink
Update kala.value
Browse files Browse the repository at this point in the history
  • Loading branch information
Glavo committed May 1, 2024
1 parent b366489 commit e1ffbf2
Show file tree
Hide file tree
Showing 34 changed files with 784 additions and 276 deletions.
2 changes: 1 addition & 1 deletion benchmark/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ val jmhVersion = "1.36"
val jolVersion = "0.17"

tasks.compileJava {
options.release.set(17)
options.release.set(21)
}

tasks.create<JavaExec>("runBenchmark") {
Expand Down
4 changes: 2 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ allprojects {
tasks.withType<Javadoc>().configureEach {
(options as StandardJavadocDocletOptions).also {
it.encoding("UTF-8")
it.addStringOption("link", "https://docs.oracle.com/en/java/javase/17/docs/api/")
it.addStringOption("link", "https://docs.oracle.com/en/java/javase/21/docs/api/")
it.addBooleanOption("html5", true)
it.addStringOption("Xdoclint:none", "-quiet")
}
Expand Down Expand Up @@ -130,7 +130,7 @@ sourceSets {
}

tasks.compileTestJava {
options.release.set(17)
options.release.set(21)
}

tasks.test {
Expand Down
15 changes: 15 additions & 0 deletions kala-base/src/main/java/kala/value/AbstractMutableValue.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
/*
* Copyright 2024 Glavo
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package kala.value;

public abstract class AbstractMutableValue<T> extends AbstractValue<T> implements MutableValue<T> {
Expand Down
22 changes: 16 additions & 6 deletions kala-base/src/main/java/kala/value/AbstractValue.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
/*
* Copyright 2024 Glavo
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package kala.value;

import java.util.Objects;
Expand All @@ -10,13 +25,8 @@ public int hashCode() {

@Override
public boolean equals(Object obj) {
if (!(obj instanceof AnyValue<?>))
return false;
AnyValue<?> other = (AnyValue<?>) obj;
if (!this.canEqual(other) || !other.canEqual(this))
return false;
return obj instanceof AnyValue<?> other && this.canEqual(other) && other.canEqual(this) && Objects.equals(this.get(), other.getValue());

return Objects.equals(this.get(), other.getValue());
}

@Override
Expand Down
18 changes: 17 additions & 1 deletion kala-base/src/main/java/kala/value/AnyValue.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,24 @@
/*
* Copyright 2024 Glavo
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package kala.value;

import kala.Equatable;
import kala.collection.base.AnyTraversable;
import kala.value.primitive.PrimitiveValue;

public interface AnyValue<T> extends AnyTraversable<T>, Equatable {
public sealed interface AnyValue<T> extends AnyTraversable<T>, Equatable permits MutableAnyValue, Value, PrimitiveValue {
T getValue();
}
20 changes: 18 additions & 2 deletions kala-base/src/main/java/kala/value/AtomicVar.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,27 @@
/*
* Copyright 2024 Glavo
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package kala.value;

import java.io.Serial;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicReference;

@SuppressWarnings("NullableProblems")
public final class AtomicVar<T> extends AtomicReference<T> implements MutableValue<T> {
@Serial
private static final long serialVersionUID = 0L;

public AtomicVar() {
Expand All @@ -21,9 +38,8 @@ public int hashCode() {

@Override
public boolean equals(Object obj) {
if (!(obj instanceof AnyValue<?>))
if (!(obj instanceof AnyValue<?> other))
return false;
AnyValue<?> other = (AnyValue<?>) obj;
if (!this.canEqual(other) || !other.canEqual(this))
return false;

Expand Down
97 changes: 57 additions & 40 deletions kala-base/src/main/java/kala/value/CheckedVar.java
Original file line number Diff line number Diff line change
@@ -1,40 +1,57 @@
package kala.value;

import java.io.Serializable;

public final class CheckedVar<T> extends AbstractMutableValue<T> implements Cloneable, Serializable {
private static final long serialVersionUID = 0L;

private final Class<T> type;
private T value;

public CheckedVar(Class<T> type) {
this.type = type;
}

public CheckedVar(Class<T> type, T value) {
this.type = type;
this.value = type.cast(value);
}

@Override
public T get() {
return value;
}

@Override
public void set(T value) throws ClassCastException {
this.value = type.cast(value);
}

@Override
@SuppressWarnings("MethodDoesntCallSuperMethod")
public CheckedVar<T> clone() {
return new CheckedVar<>(type, value);
}

@Override
public String toString() {
return "CheckedVar[" + value + "]";
}
}
/*
* Copyright 2024 Glavo
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package kala.value;

import java.io.Serial;
import java.io.Serializable;

public final class CheckedVar<T> extends AbstractMutableValue<T> implements Cloneable, Serializable {
@Serial
private static final long serialVersionUID = 0L;

private final Class<T> type;
private T value;

public CheckedVar(Class<T> type) {
this.type = type;
}

public CheckedVar(Class<T> type, T value) {
this.type = type;
this.value = type.cast(value);
}

@Override
public T get() {
return value;
}

@Override
public void set(T value) throws ClassCastException {
this.value = type.cast(value);
}

@Override
@SuppressWarnings("MethodDoesntCallSuperMethod")
public CheckedVar<T> clone() {
return new CheckedVar<>(type, value);
}

@Override
public String toString() {
return "CheckedVar[" + value + "]";
}
}
17 changes: 17 additions & 0 deletions kala-base/src/main/java/kala/value/DefaultValue.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,27 @@
/*
* Copyright 2024 Glavo
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package kala.value;

import kala.function.Memoized;

import java.io.Serial;
import java.io.Serializable;

final class DefaultValue<T> extends AbstractValue<T> implements Memoized, Serializable {
@Serial
private static final long serialVersionUID = 0L;

private final T value;
Expand Down
17 changes: 17 additions & 0 deletions kala-base/src/main/java/kala/value/DelegateMutableValue.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,29 @@
/*
* Copyright 2024 Glavo
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package kala.value;

import org.jetbrains.annotations.NotNull;

import java.io.Serial;
import java.io.Serializable;
import java.util.function.Consumer;
import java.util.function.Supplier;

final class DelegateMutableValue<T> extends AbstractMutableValue<T> implements Serializable {
@Serial
private static final long serialVersionUID = 0L;

private final @NotNull Supplier<? extends T> getter;
Expand Down
17 changes: 17 additions & 0 deletions kala-base/src/main/java/kala/value/DelegateValue.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,28 @@
/*
* Copyright 2024 Glavo
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package kala.value;

import org.jetbrains.annotations.NotNull;

import java.io.Serial;
import java.io.Serializable;
import java.util.function.Supplier;

final class DelegateValue<T> extends AbstractValue<T> implements Serializable {
@Serial
private static final long serialVersionUID = 0L;

private final @NotNull Supplier<? extends T> getter;
Expand Down
15 changes: 15 additions & 0 deletions kala-base/src/main/java/kala/value/LateInitValue.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
/*
* Copyright 2024 Glavo
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package kala.value;

public final class LateInitValue<T> implements Value<T> {
Expand Down
Loading

0 comments on commit e1ffbf2

Please sign in to comment.