Commit 1a1a267f authored by Rajiv Prabhakar's avatar Rajiv Prabhakar
Browse files

v2.0.6 release:

- Versioning info added to Internal-Readme
- DynamicConstant now throws NoSuchElementException if default is not provided, and set was never called
- DynamicConstant.isSet() added
parent 2a60f733
Pipeline #26359446 passed with stage
in 1 minute and 49 seconds
......@@ -35,6 +35,16 @@ If hitting error saying *"Signing failed: Inappropriate ioctl for device"*, run:
```export GPG_TTY=$(tty)```
Versioning
==========
Versioning generally follows [semantic versioning](https://semver.org/) rules.
One caveat: All releases with a minor version of 0, shall be considered to be in beta. As such, successive beta versions may contain breaking changes. As soon as the minor version is incremented to 1 or higher, all other semver conventions shall be followed.
Ie, version 2.0.3 may not be backward compatible with 2.0.2. But version 2.2.3 will backward compatible with both 2.2.2 and 2.1.0.
This is done to enable thorough testing of beta versions by external projects, which are unable to rely on non-semver versions.
Pending Tasks
=========
More unit tests. Most code in this library has been thoroughly tested by the [Caucus](www.thecaucus.net) project, but for more thorough testing, and to aid in future project development, it will be good to have more unit tests integrated into this repository.
......
......@@ -6,7 +6,7 @@
<groupId>com.rajivprab</groupId>
<artifactId>cava</artifactId>
<version>2.0.5</version>
<version>2.0.6</version>
<name>Cava: Clean Java</name>
<description>A library that enables users to write minimal, clean and simple Java</description>
......
......@@ -2,6 +2,7 @@ package org.rajivprab.cava;
import org.apache.commons.lang3.Validate;
import java.util.NoSuchElementException;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Supplier;
......@@ -27,10 +28,13 @@ import java.util.function.Supplier;
public class DynamicConstant<T> {
private final AtomicReference<T> value = new AtomicReference<>();
private final Supplier<T> defaultSupplier;
private T defaultSupplied;
// ------------- Constructors -----------
public static <T> DynamicConstant<T> noDefault() {
return withDefault(null);
return lazyDefault(() -> {throw new NoSuchElementException("No default provided, nor was any value set");});
}
public static <T> DynamicConstant<T> withDefault(T def) {
......@@ -42,15 +46,24 @@ public class DynamicConstant<T> {
}
private DynamicConstant(Supplier<T> defaultSupplier) {
Validatec.notNull(defaultSupplier, "Cannot have a null supplier");
this.defaultSupplier = defaultSupplier;
this.defaultSupplier = Validatec.notNull(defaultSupplier, "Cannot have a null supplier");
}
// --------------------------------------
// Returns the most recently set value, or if never set, the default.
// Throws NoSuchElementException if default was not provided, and set was never called.
public T get() {
T value = this.value.get();
return value == null ? setDefault() : value;
}
// Returns true if the value has been permanently set to any user-supplied value, or the default
// If value has never been set, the default is not invoked
public boolean isSet() {
return value.get() == null;
}
// If set is called with data that is incompatible with the previous get/set calls,
// An exception will be thrown, and no data will be changed.
public T set(T newVal) {
......@@ -71,7 +84,7 @@ public class DynamicConstant<T> {
private synchronized T setDefault() {
if (defaultSupplied == null) {
defaultSupplied = Validatec.notNull(defaultSupplier.get(), "Default/set value has not been given");
defaultSupplied = Validatec.notNull(defaultSupplier.get(), "Default value should not be null");
return set(defaultSupplied);
} else {
return defaultSupplied;
......
......@@ -3,8 +3,9 @@ package org.rajivprab.cava;
import com.google.common.collect.ImmutableList;
import org.junit.Assert;
import org.junit.Test;
import org.rajivprab.cava.exception.CheckedExceptionWrapper;
import org.rajivprab.cava.exception.InterruptedExceptionc;
import java.util.NoSuchElementException;
import java.util.Random;
import java.util.Set;
import java.util.concurrent.CountDownLatch;
......@@ -103,8 +104,8 @@ public class DynamicConstantTest extends TestBase {
try {
dynamicConstant.get();
Assert.fail();
} catch (IllegalArgumentException e) {
assertThat(e).hasMessageThat().isEqualTo("Default/set value has not been given");
} catch (NoSuchElementException e) {
assertThat(e).hasMessageThat().isEqualTo("No default provided, nor was any value set");
}
}
......@@ -115,7 +116,7 @@ public class DynamicConstantTest extends TestBase {
dynamicConstant.get();
Assert.fail();
} catch (IllegalArgumentException e) {
assertThat(e).hasMessageThat().isEqualTo("Default/set value has not been given");
assertThat(e).hasMessageThat().isEqualTo("Default value should not be null");
}
}
......@@ -149,8 +150,8 @@ public class DynamicConstantTest extends TestBase {
latch.countDown();
latch.await();
return constant.get();
} catch (Exception e) {
throw new CheckedExceptionWrapper(e);
} catch (InterruptedException e) {
throw new InterruptedExceptionc(e);
}
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment