diff --git a/src/main/java/net/openhft/chronicle/core/UnsafeMemory.java b/src/main/java/net/openhft/chronicle/core/UnsafeMemory.java index 9732c18cac4..1c22f03ae50 100644 --- a/src/main/java/net/openhft/chronicle/core/UnsafeMemory.java +++ b/src/main/java/net/openhft/chronicle/core/UnsafeMemory.java @@ -20,7 +20,6 @@ import net.openhft.chronicle.core.internal.Bootstrap; import net.openhft.chronicle.core.internal.util.DirectBufferUtil; -import net.openhft.chronicle.core.util.Ints; import net.openhft.chronicle.core.util.MisAlignedAssertionError; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -1074,29 +1073,6 @@ public void copyMemory(long srcAddress, long destAddress, long length) { } } - /** - * Copies memory from a byte array to an object. - * - * @param src source byte array. - * @param srcOffset offset of the source array from where to start copying. - * @param dest destination object. - * @param destOffset offset of the destination object from where to place the copied memory. - * @param length the length of memory to copy. - */ - @Deprecated(/* for removal in x.26, don't just delete this method, it must be inlined. */) - public void copyMemory(byte[] src, int srcOffset, @Nullable Object dest, long destOffset, int length) { - assert SKIP_ASSERTIONS || nonNull(src); - assert SKIP_ASSERTIONS || srcOffset >= 0; - assert SKIP_ASSERTIONS || destOffset >= 0; - assert SKIP_ASSERTIONS || length >= 0; - - if (dest instanceof byte[]) { - copyMemory(src, srcOffset, (byte[]) dest, Math.toIntExact(destOffset - ARRAY_BYTE_BASE_OFFSET), length); - } else { - copyMemoryLoop(src, ARRAY_BYTE_BASE_OFFSET + srcOffset, dest, destOffset, length); - } - } - /** * Copies memory from one byte array to another. * diff --git a/src/main/java/net/openhft/chronicle/core/internal/invariant/longs/LongTriCondition.java b/src/main/java/net/openhft/chronicle/core/internal/invariant/longs/LongTriCondition.java deleted file mode 100644 index eb9e04de170..00000000000 --- a/src/main/java/net/openhft/chronicle/core/internal/invariant/longs/LongTriCondition.java +++ /dev/null @@ -1,51 +0,0 @@ -/* - * Copyright 2016-2022 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.core.internal.invariant.longs; - -import net.openhft.chronicle.core.util.LongTriPredicate; - -import static java.util.Objects.requireNonNull; - -public enum LongTriCondition implements LongTriPredicate { - - BETWEEN("∈ [fromInclusive, toExclusive), where (fromInclusive, toExclusive) = ", (value, otherFirst, otherSecond) -> value >= otherFirst && value < otherSecond), - BETWEEN_CLOSED("∈ [fromInclusive, toInclusive], where (fromInclusive, toInclusive) = ", (value, otherFirst, otherSecond) -> value >= otherFirst && value <= otherSecond), - BETWEEN_ZERO_AND_ENSURING("∈ [0, index - size ], where (index, size) = ", (value, otherFirst, otherSecond) -> value >= 0 && value <= (otherFirst - otherSecond)); - - private final String operation; - private final LongTriPredicate predicate; - - LongTriCondition(final String operation, - final LongTriPredicate predicate) { - this.operation = requireNonNull(operation); - this.predicate = requireNonNull(predicate); - } - - @Override - public boolean test(final long value, - final long otherFirst, - final long otherSecond) { - return predicate.test(value, otherFirst, otherSecond); - } - - @Override - public String toString() { - return operation; - } -} diff --git a/src/main/java/net/openhft/chronicle/core/io/UnsafeText.java b/src/main/java/net/openhft/chronicle/core/io/UnsafeText.java deleted file mode 100644 index 69a2db20d0f..00000000000 --- a/src/main/java/net/openhft/chronicle/core/io/UnsafeText.java +++ /dev/null @@ -1,310 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.core.io; - -import net.openhft.chronicle.core.Jvm; -import net.openhft.chronicle.core.Maths; - -import static net.openhft.chronicle.core.UnsafeMemory.MEMORY; -import static net.openhft.chronicle.core.UnsafeMemory.UNSAFE; - -/** - * These are fast, unsafe ways to render text. - * NOTE: The caller has to ensure there is always plenty of memory to perform this operation. - */ -@Deprecated(/* to be removed in x.26 */) -public final class UnsafeText { - - public static final long MASK32 = 0xFFFF_FFFFL; - - // Suppresses default constructor, ensuring non-instantiability. - private UnsafeText() { - } - - private static final long MAX_VALUE_DIVIDE_5 = Long.MAX_VALUE / 5; - private static final String MIN_VALUE_STR = "" + Long.MIN_VALUE; - private static final long ARRAY_BYTE_BASE_OFFSET = Jvm.arrayByteBaseOffset(); - - public static long appendFixed(long address, long num) { - if (num >= 0) { - // nothing - } else if (num > Long.MIN_VALUE) { - MEMORY.writeByte(address++, (byte) '-'); - num = -num; - } else { - return appendText(address, MIN_VALUE_STR); - } - - long start = address; - do { - long div = num / 10; - long mod = num % 10; - MEMORY.writeByte(address++, (byte) ('0' + mod)); - num = div; - } while (num > 0); - // reverse the order - reverseTheOrder(address, start); - return address; - } - - private static void reverseTheOrder(long address, long start) { - int end = (int) (address - start) - 1; - for (int i = 0; i < end; i++, end--) { - long a1 = start + i; - long a2 = start + end; - byte b1 = UNSAFE.getByte(a1); - byte b2 = UNSAFE.getByte(a2); - MEMORY.writeByte(a2, b1); - MEMORY.writeByte(a1, b2); - } - } - - public static long appendFixed(long address, double num, int digits) { - long tens = Maths.tens(digits); - double mag = num * tens; - if (Math.abs(mag) < 1L << 53) { - long num2 = Math.round(mag); - return appendBase10d(address, num2, digits); - } else { - return appendDouble(address, num); - } - } - - public static long appendBase10d(long address, long num, int decimal) { - if (num >= 0) { - // nothing - } else if (num > Long.MIN_VALUE) { - MEMORY.writeByte(address++, (byte) '-'); - num = -num; - } else { - throw new AssertionError(); - } - - long start = address; - do { - long div = num / 10; - long mod = num % 10; - MEMORY.writeByte(address++, (byte) ('0' + mod)); - if (--decimal == 0) - MEMORY.writeByte(address++, (byte) '.'); - num = div; - } while (num > 0 || decimal >= 0); - // reverse the order - reverseTheOrder(address, start); - return address; - } - - /** - * Internal method for low level appending a String. The caller must ensure there is at least 32 bytes available. - * - * @param address to start writing - * @param d double value - * @return endOfAddress - */ - // throws BufferOverflowException, IllegalArgumentException - public static long appendDouble(long address, double d) { - double abs = Math.abs(d); - // outside range so that !Double.isFinite(d) implicitly added. - if (6e-8 > abs || abs > 1e31) { - return appendDoubleString(address, d); - } else { - return appendDouble0(address, d); - } - } - - static long appendDouble0(long address, double d) { - long val = Double.doubleToRawLongBits(d); - int sign = (int) (val >>> 63); - int exp = (int) ((val >>> 52) & 2047); - long mantissa = val & ((1L << 52) - 1); - if (sign != 0) { - MEMORY.writeByte(address++, (byte) '-'); - } - if (exp == 0 && mantissa == 0) { - MEMORY.writeByte(address, (byte) '0'); - UNSAFE.putShort(address + 1, (short) ('.' + ('0' << 8))); - address += 3; - return address; - - } else if (exp == 2047) { - return appendText(address, - mantissa == 0 ? "Infinity" : "NaN"); - - } else if (exp > 0) { - mantissa += 1L << 52; - } - final int shift = (1023 + 52) - exp; - - if (shift > 0) { - // integer and faction - if (shift < 53) { - return appendIntegerAndFraction(address, d, sign, mantissa, shift); - - } else { - return appendFraction(address, d, sign, mantissa, shift); - } - } - // large number - return appendLargeNumber(address, mantissa, shift); - } - - static final ThreadLocal TL_SB = ThreadLocal.withInitial(StringBuilder::new); - static long appendDoubleString(long address, double d) { - StringBuilder sb = TL_SB.get(); - sb.setLength(0); - sb.append(d); - for (int i = 0; i < sb.length(); i++) - UNSAFE.putByte(address++, (byte) sb.charAt(i)); - return address; - } - - private static long appendLargeNumber(long address, long mantissa, int shift) { - mantissa <<= 10; - int precision = -10 - shift; - int digits = 0; - while ((precision > 53 || mantissa > Long.MAX_VALUE >> precision) && precision > 0) { - digits++; - precision--; - long mod = mantissa % 5; - mantissa /= 5; - int modDiv = 1; - while (mantissa < MAX_VALUE_DIVIDE_5 && precision > 1) { - precision -= 1; - mantissa <<= 1; - modDiv <<= 1; - } - mantissa += modDiv * mod / 5; - } - long val2 = precision > 0 ? mantissa << precision : mantissa >>> -precision; - - address = appendFixed(address, val2); - for (int i = 0; i < digits; i++) - MEMORY.writeByte(address++, (byte) '0'); - return address; - } - - private static long appendFraction(long address, double d, int sign, long mantissa, int shift) { - long value = 0; - int digits = -1; - mantissa = mantissa << 9; - shift += 9; - do { - if (shift < 63) { - value = value * 10 + (mantissa >> shift); - mantissa &= (1L << shift) - 1; - } - if (mantissa >= Long.MAX_VALUE / 5) { - mantissa >>>= 3; - shift -= 3; - } - // times 10 - mantissa *= 5; - shift--; - digits++; - } while (value < 1e17); - long value3 = value; - // back track - while (true) { - final long value2 = (value + 5) / 10; - final double parsedValue = Maths.asDouble(value2, 0, sign != 0, digits - 1); - if (parsedValue != d) - break; - digits--; - value3 = value2; - value = value / 10; - } - - UNSAFE.putShort(address, (short) ('0' + ('.' << 8))); - address += 2; - - long addressOfLastNonZero = address + digits; - - do { - long num = value3 % 10; - value3 /= 10; - final char c = (char) ('0' + num); - digits--; - MEMORY.writeByte(address + digits, (byte) c); - } while (value3 > 0); - while (digits > 0) { - digits--; - UNSAFE.putByte(address + digits, (byte) '0'); - } - - return addressOfLastNonZero; - } - - private static long appendIntegerAndFraction(long address, double d, int sign, long mantissa, int shift) { - long intValue = mantissa >> shift; - address = appendFixed(address, intValue); - mantissa -= intValue << shift; - if (mantissa > 0) { - MEMORY.writeByte(address++, (byte) '.'); - mantissa <<= 1; - mantissa++; - int precision = shift + 1; - long error = 1; - - long value = intValue; - int decimalPlaces = 0; - while (mantissa > error) { - // times 5*2 = 10 - mantissa *= 5; - error *= 5; - precision--; - long num = (mantissa >> precision); - value = value * 10 + num; - MEMORY.writeByte(address++, (byte) ('0' + num)); - mantissa -= num << precision; - - final double parsedValue = Maths.asDouble(value, 0, sign != 0, ++decimalPlaces); - if (parsedValue == d) - break; - } - } else { - UNSAFE.putShort(address, (short) ('.' + ('0' << 8))); - address += 2; - } - return address; - } - - private static long appendText(long address, String s) { - for (int i = 0; i < s.length(); i++) { - MEMORY.writeByte(address++, (byte) s.charAt(i)); - } - return address; - } - - public static long append8bit(long address, byte[] bytes) { - final int len = bytes.length; - int i; - for (i = 0; i < len - 7; i += 8) - MEMORY.writeLong(address + i, UNSAFE.getLong(bytes, ARRAY_BYTE_BASE_OFFSET + i)); - for (; i < len; i++) - MEMORY.writeByte(address + i, UNSAFE.getByte(bytes, ARRAY_BYTE_BASE_OFFSET + i)); - return address + len; - } - - public static long append8bit(long address, char[] chars) { - final int len = chars.length; - int i; - for (i = 0; i < len; i++) - MEMORY.writeByte(address + i, (byte) chars[i]); - return address + len; - } -} diff --git a/src/main/java/net/openhft/chronicle/core/threads/JitterSampler.java b/src/main/java/net/openhft/chronicle/core/threads/JitterSampler.java deleted file mode 100644 index edd3a79a7de..00000000000 --- a/src/main/java/net/openhft/chronicle/core/threads/JitterSampler.java +++ /dev/null @@ -1,111 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.core.threads; - -import net.openhft.chronicle.core.Jvm; - -import java.util.concurrent.TimeUnit; - -/** - * A utility class for profiling and tracking the execution stages of threads. - *

- * This class can be used to take snapshots of a thread's stack trace at different stages - * and measure how long the thread has been blocked. - * - */ -@Deprecated(/* to be moved in x.26 */) -public final class JitterSampler { - private JitterSampler() { - } - - public static final String PROFILE_OF_THE_THREAD = "profile of the thread"; - public static final String THREAD_HAS_BLOCKED_FOR = "thread has blocked for"; - - static final long JITTER_THRESHOLD = - TimeUnit.MILLISECONDS.toNanos( - Jvm.getLong("chronicle.jitter.threshold", 10L)); - static volatile String desc; - static volatile Thread thread; - static volatile long time = Long.MAX_VALUE; - - /** - * Marks the current stage of the thread for profiling. - * - * @param desc a description of the current stage - */ - public static void atStage(String desc) { - Jvm.startup().on(JitterSampler.class, "atStage " + desc); - JitterSampler.desc = desc; - thread = Thread.currentThread(); - time = System.nanoTime(); - } - - /** - * Takes a snapshot of the current thread state if the thread has been - * blocked longer than the specified threshold. - * - * @return a String representation of the thread's stack trace and the time blocked, - * or null if the thread has been blocked less than the threshold. - */ - public static String takeSnapshot() { - return takeSnapshot(JITTER_THRESHOLD); - } - - /** - * Takes a snapshot of the current thread state if the thread has been - * blocked longer than the specified threshold. - * - * @param threshold the time threshold in nanoseconds - * @return a String representation of the thread's stack trace and the time blocked, - * or null if the thread has been blocked less than the threshold. - */ - public static String takeSnapshot(long threshold) { - long time = JitterSampler.time; - long now = System.nanoTime(); - if (time > now - threshold) - return null; - Thread thread = JitterSampler.thread; - String desc = JitterSampler.desc; - if (thread == null || desc == null) - return null; - StackTraceElement[] stes = thread.getStackTrace(); - if (stes.length < 1) - return null; - StringBuilder sb = new StringBuilder(); - sb.append(PROFILE_OF_THE_THREAD) - .append(' ').append(thread.getName()) - .append(' ').append(desc) - .append(" " + THREAD_HAS_BLOCKED_FOR + " ").append((now - time) / 1000_000) - .append(" ms\n"); - for (StackTraceElement ste : stes) { - sb.append("\tat ").append(ste).append('\n'); - } - return sb.toString(); - } - - /** - * Marks the current thread as finished for profiling. - */ - public static void finished() { - Jvm.startup().on(JitterSampler.class, "finished"); - thread = null; - desc = "finished"; - time = Long.MAX_VALUE; - } - -} diff --git a/src/main/java/net/openhft/chronicle/core/threads/MonitorProfileAnalyserMain.java b/src/main/java/net/openhft/chronicle/core/threads/MonitorProfileAnalyserMain.java deleted file mode 100644 index 9192866b1e6..00000000000 --- a/src/main/java/net/openhft/chronicle/core/threads/MonitorProfileAnalyserMain.java +++ /dev/null @@ -1,126 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.core.threads; - -import net.openhft.chronicle.core.Jvm; - -import java.io.BufferedReader; -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Paths; -import java.util.*; -import java.util.stream.Collectors; - -@Deprecated(/* to be moved in x.26 */) -public class MonitorProfileAnalyserMain { - - private static final int MAX_LINES = Jvm.getInteger("st.maxlines", 8); - private static final String PROFILE_OF_THE_THREAD = "profile of the thread"; - private static final String THREAD_HAS_BLOCKED_FOR = "thread has blocked for"; - - /** - * Reads one or more log files and looks for thread profiles to summarise - */ - public static void main(String[] args) throws IOException { - if (args.length == 0) - System.err.println("No input file(s) provided"); - - final String stIgnore = Jvm.getProperty("st.ignore"); - List ignoreSubStrings = stIgnore != null ? Arrays.asList(stIgnore.split(",")) : Collections.emptyList(); - int interval = Integer.getInteger("interval", 0); - if (interval <= 0) { - main0(ignoreSubStrings, args); - } else { - for (; ; ) { - main0(ignoreSubStrings, args); - Jvm.pause(interval * 1000L); - System.out.println("\n---\n"); - } - } - } - - public static void main0(List ignoreSubStrings, String[] args) throws IOException { - System.out.println("Grouped by line"); - Map stackCount = new LinkedHashMap<>(); - - for (String arg : args) { - StringBuilder sb = new StringBuilder(); - int lineCount = -1; - try (BufferedReader br = Files.newBufferedReader(Paths.get(arg))) { - // TODO: PrintGCApplicationStoppedTime - - for (String line; (line = br.readLine()) != null; ) { - if (line.contains(PROFILE_OF_THE_THREAD) || line.contains(THREAD_HAS_BLOCKED_FOR)) { - if (sb.length() > 0) { - addToStackCount(ignoreSubStrings, stackCount, sb); - } - lineCount = 0; - sb.setLength(0); - - } else if (partOfStackTrace(line) && lineCount >= 0) { - if (++lineCount <= MAX_LINES) { - sb.append(line).append("\n"); - } - } else if (sb.length() > 0) { - addToStackCount(ignoreSubStrings, stackCount, sb); - sb.setLength(0); - } - } - } - } - List> stackSortedByCount = - stackCount.entrySet().stream() - .filter(e -> e.getValue() > 2) - .sorted(Comparator.comparing(e -> -e.getValue())) // reversed - .limit(20) - .collect(Collectors.toList()); - stackSortedByCount.forEach(e -> { - stackCount.remove(e.getKey()); - System.out.println(e.getValue() + e.getKey()); - }); - - System.out.println("Grouped by method."); - Map methodCount = new LinkedHashMap<>(); - for (Map.Entry entry : stackCount.entrySet()) { - String stack = entry.getKey().replaceFirst("\\(.*?\\)", "( * )"); - methodCount.compute(stack, (k, v) -> (v == null ? 0 : v) + entry.getValue()); - } - - List> methodSortedByCount = - methodCount.entrySet().stream() - .filter(e -> e.getValue() > 2) - .sorted(Comparator.comparing(e -> -e.getValue())) // reversed - .limit(20) - .collect(Collectors.toList()); - methodSortedByCount - .forEach(e -> System.out.println(e.getValue() + e.getKey())); - } - - private static void addToStackCount(List ignoreSubStrings, Map stackCount, StringBuilder sb) { - String lines = sb.toString(); - for (String ss : ignoreSubStrings) - if (lines.contains(ss)) - return; - stackCount.compute(lines, (k, v) -> v == null ? 1 : v + 1); - } - - private static boolean partOfStackTrace(String line) { - // make this more robust in the face of copy/pasting etc. - return line.startsWith("\tat ") || line.matches("\\s+at .*"); - } -} diff --git a/src/main/java/net/openhft/chronicle/core/threads/StackSampler.java b/src/main/java/net/openhft/chronicle/core/threads/StackSampler.java deleted file mode 100644 index d03e5749f39..00000000000 --- a/src/main/java/net/openhft/chronicle/core/threads/StackSampler.java +++ /dev/null @@ -1,97 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.core.threads; - -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; - -import java.util.concurrent.locks.LockSupport; - -/** - * A utility class for sampling the stack traces of a target thread. - * This class creates a background daemon thread which periodically - * samples the stack trace of the specified thread and stores the - * latest snapshot. - */ -@Deprecated(/* to be moved in x.26 */) -public class StackSampler { - @NotNull - private final Thread sampler; - - private volatile Thread thread = null; - private volatile StackTraceElement[] stack = null; - - /** - * Constructs a new StackSampler and starts the background thread - * responsible for sampling the stack trace. - */ - public StackSampler() { - sampler = new Thread(this::sampling, "Thread sampler"); - sampler.setDaemon(true); - sampler.start(); - } - - /** - * Continuously samples the stack trace of the target thread at - * periodic intervals. This method is internally used by the background - * thread created in the constructor. - */ - void sampling() { - while (!Thread.currentThread().isInterrupted()) { - Thread t = thread; - if (t != null) { - StackTraceElement[] stack0 = t.getStackTrace(); - if (thread == t) - stack = stack0; - } - LockSupport.parkNanos(10_000); - } - } - - /** - * Stops the stack sampling by interrupting the background thread. - */ - public void stop() { - sampler.interrupt(); - } - - /** - * Sets the thread to be sampled. - * - * @param thread the target thread whose stack trace should be sampled. - */ - public void thread(Thread thread) { - this.thread = thread; - } - - /** - * Retrieves the latest sampled stack trace and resets the internal - * state for subsequent sampling. - * - * @return the latest stack trace sampled or null if no stack trace was sampled. - */ - @Nullable - public StackTraceElement[] getAndReset() { - final StackTraceElement[] lStack = this.stack; - thread = null; - this.stack = null; - return lStack; - } -} diff --git a/src/main/java/net/openhft/chronicle/core/threads/ThreadLock.java b/src/main/java/net/openhft/chronicle/core/threads/ThreadLock.java deleted file mode 100644 index abdb874a30b..00000000000 --- a/src/main/java/net/openhft/chronicle/core/threads/ThreadLock.java +++ /dev/null @@ -1,91 +0,0 @@ -/* - * Copyright 2016-2022 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.core.threads; - -import net.openhft.posix.PosixAPI; - -@Deprecated(/* to be moved in x.26 */) -public interface ThreadLock { - - /** - * Try to lock once or fail using the current OS thread id. - * - * @return true if the lock could be obtained, or false if not. - * @throws IllegalStateException if the same threadId already holds the lock - */ - default boolean tryLock() throws IllegalStateException { - return tryLock(gettid()); - } - - /** - * @return the thread id used for locking for the current thread, or -1 is unknown. - */ - default int gettid() { - try { - return PosixAPI.posix().gettid(); - } catch (Error e) { - return -1; - } - } - - /** - * Try to lock once or fail - * - * @param osThreadId to attempt to lock for - * @return true if the lock could be obtained, or false if not. - * @throws IllegalStateException if the same threadId already holds the lock - */ - boolean tryLock(int osThreadId) throws IllegalStateException; - - /** - * Lock the resource using the current OS thread id. - * - * @throws InterruptedRuntimeException if an interrupt occurred before or during a busy loop retry. It won't throw this if the lock can be obtained immediately. - * @throws IllegalStateException if the same threadId already holds the lock - */ - default void lock() throws InterruptedRuntimeException, IllegalStateException { - lock(gettid()); - } - - /** - * Lock the resource using a threadId - * - * @param osThreadId to lock - * @throws InterruptedRuntimeException if an interrupt occurred before or during a busy loop retry. It won't throw this if the lock can be obtained immediately. - * @throws IllegalStateException if the same threadId already holds the lock - */ - void lock(int osThreadId) throws InterruptedRuntimeException, IllegalStateException; - - /** - * Unlock for a threadId - * - * @throws IllegalStateException if the thread previously held the lock but doesn't hold it now. - */ - default void unlock() throws IllegalStateException { - unlock(gettid()); - } - - /** - * Unlock for a threadId - * - * @param osThreadId to unlock - * @throws IllegalStateException if the thread previously held the lock but doesn't hold it now. - */ - void unlock(int osThreadId) throws IllegalStateException; -} diff --git a/src/main/java/net/openhft/chronicle/core/util/IntBiPredicate.java b/src/main/java/net/openhft/chronicle/core/util/IntBiPredicate.java index 9ce6895ddb7..e239c88152e 100644 --- a/src/main/java/net/openhft/chronicle/core/util/IntBiPredicate.java +++ b/src/main/java/net/openhft/chronicle/core/util/IntBiPredicate.java @@ -27,6 +27,7 @@ * * @see Predicate */ +@Deprecated(/*to be removed in x.27*/) @FunctionalInterface public interface IntBiPredicate { diff --git a/src/main/java/net/openhft/chronicle/core/util/IntTriPredicate.java b/src/main/java/net/openhft/chronicle/core/util/IntTriPredicate.java index efe90ec2251..8043145bbc3 100644 --- a/src/main/java/net/openhft/chronicle/core/util/IntTriPredicate.java +++ b/src/main/java/net/openhft/chronicle/core/util/IntTriPredicate.java @@ -27,6 +27,7 @@ * * @see Predicate */ +@Deprecated(/*to be removed in x.27*/) @FunctionalInterface public interface IntTriPredicate { diff --git a/src/main/java/net/openhft/chronicle/core/util/Ints.java b/src/main/java/net/openhft/chronicle/core/util/Ints.java index 27e67e1d8e9..838e1aa4b45 100644 --- a/src/main/java/net/openhft/chronicle/core/util/Ints.java +++ b/src/main/java/net/openhft/chronicle/core/util/Ints.java @@ -62,190 +62,7 @@ private Ints() { */ public static int requireNonNegative(final int value) { if (value < 0) - throw new IllegalArgumentException(failDescription(negative().negate(), value)); - return value; - } - - /** - * Returns the provided {@code value} after checking that it satisfies the provided {@code requirement} throwing - * an {@link IllegalArgumentException} if the check fails. - *

- * This method is designed primarily for doing parameter validation in public methods - * and constructors, as demonstrated below: - *

-     * public Foo(int bar) {
-     *     this.bar = require(nonNegative(), bar);
-     * }
-     * 
- * - * @param requirement to impose on the provided {@code value} - * @param value the value to check - * @return the provided {@code value} if the check passes - * @throws NullPointerException if the provided {@code requirement} is {@code null}. - * @throws IllegalArgumentException if the check fails - */ - @Deprecated(/* to be removed in x.26 */) - public static int require(final IntPredicate requirement, - final int value) { - return require(requirement, value, IllegalArgumentException::new); - } - - /** - * Returns the provided {@code value} after checking that it satisfies the provided {@code requirement} throwing - * a custom exception if the check fails. - *

- * This method is designed primarily for doing parameter validation in public methods - * and constructors, as demonstrated below: - *

-     * public Foo(int bar) {
-     *     this.bar = require(byteConvertible(), bar, ArithmeticException::new);
-     * }
-     * 
- * - * @param Exception typ to throw if the check fails - * @param requirement to impose on the provided {@code value} - * @param value the value to check - * @param exceptionMapper to apply should the check fail - * @return the provided {@code value} if the check passes - * @throws NullPointerException if the provided {@code requirement} is {@code null} or if the provided - * {@code exceptionMapper} is {@code null}. - * @throws RuntimeException of the specified type of the provided {@code exceptionMapper} - */ - @Deprecated(/* to be removed in x.26 */) - public static int require(final IntPredicate requirement, - final int value, - final Function exceptionMapper) { - requireNonNull(exceptionMapper); - if (!requirement.test(value)) - throw exceptionMapper.apply(failDescription(requirement, value)); - return value; - } - - /** - * Returns the provided {@code value} after checking that it and the provided {@code otherValue} satisfies the - * provided {@code requirement} throwing an {@link IllegalArgumentException} if the check fails. - *

- * Checks that the provided {@code value} and provided {@code otherValue} satisfies the provided {@code requirement}. - *

- * This method is designed primarily for doing parameter validation in public methods - * and constructors, as demonstrated below: - *

-     * public Foo(int bar) {
-     *     this.bar = require(betweenZeroAnd(), bar, 32);
-     * }
-     * 
- * - * @param requirement to impose on the provided {@code value} and {@code otherValue} - * @param value the value to check - * @param otherValue the other value to compare against the provided {@code value} - * @return the provided {@code value} if the check passes - * @throws NullPointerException if the provided {@code requirement} is {@code null}. - * @throws IllegalArgumentException if the check fails - */ - @Deprecated(/* to be removed in x.26 */) - public static int require(final IntBiPredicate requirement, - final int value, - final int otherValue) { - if (!requirement.test(value, otherValue)) - throw new IllegalArgumentException(failDescription(requirement, value, otherValue)); - return value; - } - - /** - * Returns the provided {@code value} after checking that it and the provided {@code otherValue} satisfies the - * provided {@code requirement} throwing an {@link IllegalArgumentException} if the check fails. - *

- * Checks that the provided {@code value} and provided {@code otherValue} satisfies the provided {@code requirement}. - *

- * This method is designed primarily for doing parameter validation in public methods - * and constructors, as demonstrated below: - *

-     * public Foo(int bar) {
-     *     this.bar = require(betweenZeroAnd(), bar, 16, 32, ArithmeticException::new);
-     * }
-     * 
- * - * @param Exception typ to throw if the check fails - * @param requirement to impose on the provided {@code value} and {@code otherValue} - * @param value the value to check - * @param otherValue the other value to compare against the provided {@code value} - * @param exceptionMapper to apply should the check fail - * @return the provided {@code value} if the check passes - * @throws NullPointerException if the provided {@code requirement} is {@code null}. - * @throws IllegalArgumentException if the check fails - */ - @Deprecated(/* to be removed in x.26 */) - public static int require(final IntBiPredicate requirement, - final int value, - final int otherValue, - final Function exceptionMapper) { - requireNonNull(exceptionMapper); - if (!requirement.test(value, otherValue)) - throw exceptionMapper.apply(failDescription(requirement, value, otherValue)); - return value; - } - - /** - * Returns the provided {@code value} after checking that it and the provided {@code otherValue} - * and the provided {@code otherSecondValue} satisfies the provided {@code requirement} - * throwing an {@link IllegalArgumentException} if the check fails. - *

- * This method is designed primarily for doing parameter validation in public methods - * and constructors, as demonstrated below: - *

-     * public Foo(int bar) {
-     *     this.bar = require(between(), bar, 16, 32);
-     * }
-     * 
- * - * @param requirement to impose on the provided values - * @param value the value to check - * @param otherFirstValue the other first value to compare against the provided {@code value} - * @param otherSecondValue the other first value to compare against the provided {@code value} - * @return the provided {@code value} if the check passes - * @throws NullPointerException if the provided {@code requirement} is {@code null}. - * @throws IllegalArgumentException if the check fails - */ - @Deprecated(/* to be removed in x.26 */) - public static int require(final IntTriPredicate requirement, - final int value, - final int otherFirstValue, - final int otherSecondValue) { - return require(requirement, value, otherFirstValue, otherSecondValue, IllegalArgumentException::new); - } - - /** - * Returns the provided {@code value} after checking that it and the provided {@code otherValue} - * and the provided {@code otherSecondValue} satisfies the provided {@code requirement} - * throwing an {@link IllegalArgumentException} if the check fails. - *

- * This method is designed primarily for doing parameter validation in public methods - * and constructors, as demonstrated below: - *

-     * public Foo(int bar) {
-     *     this.bar = require(between(), bar, 16, 32, ArithmeticException::new);
-     * }
-     * 
- * - * @param Exception typ to throw if the check fails - * @param requirement to impose on the provided values - * @param value the value to check - * @param otherFirstValue the other first value to compare against the provided {@code value} - * @param otherSecondValue the other first value to compare against the provided {@code value} - * @param exceptionMapper to apply should the check fail - * @return the provided {@code value} if the check passes - * @throws NullPointerException if the provided {@code requirement} is {@code null}. - * @throws IllegalArgumentException if the check fails - */ - @Deprecated(/* to be removed in x.26 */) - public static int require(final IntTriPredicate requirement, - final int value, - final int otherFirstValue, - final int otherSecondValue, - final Function exceptionMapper) { - requireNonNull(exceptionMapper); - if (!requirement.test(value, otherFirstValue, otherSecondValue)) - throw exceptionMapper.apply(failDescription(requirement, value, otherFirstValue, otherSecondValue)); + throw new IllegalArgumentException("The provided value (" + value + ") is negative"); return value; } @@ -278,38 +95,6 @@ public static boolean assertIfEnabled(final IntPredicate requirement, return true; } - /** - * Asserts that the provided {@code value} and provided {@code otherValue} satisfies the provided {@code requirement} - * if assertions is enabled (i.e. {@code -ea}) and {@link AssertUtil#SKIP_ASSERTIONS} is {@code false}. - *

- * This method is designed primarily for doing parameter validation in private methods - * and constructors, as demonstrated below: - *

-     * private Foo(int bar) {
-     *     assertIfEnabled(betweenZeroAnd(), bar, 32);
-     *     this.bar = bar;
-     * }
-     * 
- * - * @param requirement to impose on the provided {@code value} and {@code otherValue} - * @param value the value to check - * @param otherValue the other value to compare against the provided {@code value} - * @return {@code true} - * @throws NullPointerException if the provided {@code requirement} is {@code null}. There is no guarantee that this - * exception is thrown. For example, if assertions are not enabled, then the exception - * might not be thrown. - * @throws AssertionError if the check fails and assertions are enabled both via the {@code -ea} JVM command - * line option and by setting {@link AssertUtil#SKIP_ASSERTIONS} to {@code false}. - */ - @Deprecated(/* to be removed in x.26 */) - public static boolean assertIfEnabled(final IntBiPredicate requirement, - final int value, - final int otherValue) { - assert AssertUtil.SKIP_ASSERTIONS || requirement.test(value, otherValue) - : failDescription(requirement, value, otherValue); - return true; - } - /** * Asserts that the provided {@code value}, provided {@code otherFirstValue} and provided {@code otherSecondValue} * satisfies the provided {@code requirement} if assertions is enabled (i.e. {@code -ea}) and @@ -394,26 +179,6 @@ public static String failDescription(final IntTriPredicate requirement, return String.format("The provided value (%d) is illegal because it does not satisfy the provided requirement: %d %s (%d, %d)", value, value, requirement, otherFirstValue, otherSecondValue); } - /** - * Returns a predicate that can test if a value is positive (i.e. value > 0). - * - * @return a predicate that can test if a value is positive (i.e. value > 0) - */ - @Deprecated(/* to be removed in x.26 */) - public static IntPredicate positive() { - return IntCondition.POSITIVE; - } - - /** - * Returns a predicate that can test if a value is negative (i.e. value < 0). - * - * @return a predicate that can test if a value is negative (i.e. value < 0) - */ - @Deprecated(/* to be removed in x.26 */) - public static IntPredicate negative() { - return IntCondition.NEGATIVE; - } - /** * Returns a predicate that can test if a value is non-negative (i.e. value >= 0). *

@@ -424,233 +189,4 @@ public static IntPredicate negative() { public static IntPredicate nonNegative() { return IntCondition.NON_NEGATIVE; } - - /** - * Returns a predicate that can test if a value is zero (i.e. value == 0). - * - * @return a predicate that can test if a value is zero (i.e. value == 0) - */ - @Deprecated(/* to be removed in x.26 */) - public static IntPredicate zero() { - return IntCondition.ZERO; - } - - /** - * Returns a predicate that can test if a value can fit in a {@code byte} (i.e. value ∈ [-128, 127]"). - * - * @return a predicate that can test if a value can fit in a {@code byte} (i.e. value ∈ [-128, 127]") - */ - @Deprecated(/* to be removed in x.26 */) - public static IntPredicate byteConvertible() { - return IntCondition.BYTE_CONVERTIBLE; - } - - /** - * Returns a predicate that can test if a value can fit in a {@code short} (i.e. value ∈ [-32768, 32767]"). - * - * @return a predicate that can test if a value can fit in a {@code short} (i.e. value ∈ [-32768, 32767]") - */ - @Deprecated(/* to be removed in x.26 */) - public static IntPredicate shortConvertible() { - return IntCondition.SHORT_CONVERTIBLE; - } - - /** - * Returns a predicate that can test if a value is an even power of two - * (i.e. log2(value) is an integer). - * - * @return a predicate that can test if a value is an even power of two - * (i.e. log2(value) is an integer) - */ - @Deprecated(/* to be removed in x.26 */) - public static IntPredicate evenPowerOfTwo() { - return IntCondition.EVEN_POWER_OF_TWO; - } - - /** - * Returns a predicate that can test if a value is short aligned - * (i.e. value & (Short.BYTES - 1) == 0). - * - * @return a predicate that can test if a value is short aligned - * (i.e. value & (Short.BYTES - 1) == 0) - */ - @Deprecated(/* to be removed in x.26 */) - public static IntPredicate shortAligned() { - return IntCondition.SHORT_ALIGNED; - } - - /** - * Returns a predicate that can test if a value is int aligned - * (i.e. value & (Integer.BYTES - 1) == 0). - * - * @return a predicate that can test if a value is a int aligned - * (i.e. value & (Integer.BYTES - 1) == 0) - */ - @Deprecated(/* to be removed in x.26 */) - public static IntPredicate intAligned() { - return IntCondition.INT_ALIGNED; - } - - /** - * Returns a predicate that can test if a value is long aligned - * (i.e. value & (Long.BYTES - 1) == 0). - * - * @return a predicate that can test if a value is a long aligned - * (i.e. value & (Long.BYTES - 1) == 0) - */ - @Deprecated(/* to be removed in x.26 */) - public static IntPredicate longAligned() { - return IntCondition.LONG_ALIGNED; - } - - /** - * Returns a predicate that can test if a value is equal to another value. - * - * @return a predicate that can test if a value is equal to another value - */ - @Deprecated(/* to be removed in x.26 */) - public static IntBiPredicate equalTo() { - return IntBiCondition.EQUAL_TO; - } - - /** - * Returns a predicate that can test if a value is greater than to another value. - * - * @return a predicate that can test if a value is greater than to another value - */ - @Deprecated(/* to be removed in x.26 */) - public static IntBiPredicate greaterThan() { - return IntBiCondition.GREATER_THAN; - } - - /** - * Returns a predicate that can test if a value is greater or equal to another value. - * - * @return a predicate that can test if a value is greater or equal to another value - */ - @Deprecated(/* to be removed in x.26 */) - public static IntBiPredicate greaterOrEqual() { - return IntBiCondition.GREATER_OR_EQUAL; - } - - /** - * Returns a predicate that can test if a value is less than to another value. - * - * @return a predicate that can test if a value is less than to another value - */ - @Deprecated(/* to be removed in x.26 */) - public static IntBiPredicate lessThan() { - return IntBiCondition.LESS_THAN; - } - - /** - * Returns a predicate that can test if a value is less or equal to another value. - * - * @return a predicate that can test if a value is less or equal to another value - */ - @Deprecated(/* to be removed in x.26 */) - public static IntBiPredicate lessOrEqual() { - return IntBiCondition.LESS_OR_EQUAL; - } - - /** - * Returns a predicate that can test if a value is between zero and another value (exclusive) - * (i.e value ∈ [0, other value) ). - * - * @return a predicate that can test if a value is between zero and another value (exclusive) - * (i.e value ∈ [0, other value) ) - */ - @Deprecated(/* to be removed in x.26 */) - public static IntBiPredicate betweenZeroAnd() { - return IntBiCondition.BETWEEN_ZERO_AND; - } - - /** - * Returns a predicate that can test if a value is between zero and another value (inclusive) - * (i.e value ∈ [0, other value] ). - * - * @return a predicate that can test if a value is between zero and another value (inclusive) - * (i.e value ∈ [0, other value] ) - */ - @Deprecated(/* to be removed in x.26 */) - public static IntBiPredicate betweenZeroAndClosed() { - return IntBiCondition.BETWEEN_ZERO_AND_CLOSED; - } - - /** - * Returns a predicate that can test if a value is a power of two of another value - * (i.e. value = 2 ^ other value). - * For example, - * {@code powerOfTwo().test(16, 4)} is {@code true} because 2^4 is 16 - * - * @return a predicate that can test if a value is an even power of two - * (i.e. an N exists such that value ^ N is an integer) - */ - @Deprecated(/* to be removed in x.26 */) - public static IntBiPredicate powerOfTwo() { - return IntBiCondition.POWER_OF_TWO; - } - - /** - * Returns a predicate that can test if a value is log2 of another value - * (i.e. value = log2(other value) ). - *

- * For example, - * {@code log2().test(4, 16)} is {@code true} because log2(16) is 4 - * - * @return a predicate that can test if a value is log2 of another value - * (i.e. value = log2(other value) ). - */ - @Deprecated(/* to be removed in x.26 */) - public static IntBiPredicate log2() { - return IntBiCondition.LOG2; - } - - /** - * Returns a predicate that can test if a value is between another first value (inclusive) - * and another second value (exclusive) (i.e value ∈ [other first value , other second value) ). - * - * @return a predicate that can test if a value is between another first value (inclusive) - * and another second value (exclusive) (i.e value ∈ [other first value , other second value) ) - */ - @Deprecated(/* to be removed in x.26 */) - public static IntTriPredicate between() { - return IntTriCondition.BETWEEN; - } - - /** - * Returns a predicate that can test if a value is between (closed) another first value (inclusive) - * and another second value (inclusive) (i.e value ∈ [other first value , other second value] ). - * - * @return a predicate that can test if a value is between (closed) another first value (inclusive) - * and another second value (inclusive) (i.e value ∈ [other first value , other second value] ). - */ - @Deprecated(/* to be removed in x.26 */) - public static IntTriPredicate betweenClosed() { - return IntTriCondition.BETWEEN_CLOSED; - } - - /** - * Returns a predicate that can test if a value is between zero and another first value (inclusive) - * whilst ensuring a value of size defined by another second value can fit. - * (i.e value ∈ [0, other first value - other second value] ). - *

- * This predicate is useful when ensuring that a memory structure can be updated without exceeding its upper - * bounds. For example: - *

-     * public static void putInt(byte[] bytes, int offset, int value) {
-     *         assert nonNull(bytes);
-     *         assert betweenZeroAndReserving.test(offset, bytes.length, Integer.BYTES);
-     *         ...
-     * 
- * - * @return Returns a predicate that can test if a value is between zero and another first value (inclusive) - * whilst ensuring a value of size defined by another second value can fit. - * (i.e value ∈ [0, other first value - other second value] ). - */ - @Deprecated(/* to be removed in x.26 */) - public static IntTriPredicate betweenZeroAndReserving() { - return IntTriCondition.BETWEEN_ZERO_AND_ENSURING; - } - } diff --git a/src/main/java/net/openhft/chronicle/core/util/LongBiPredicate.java b/src/main/java/net/openhft/chronicle/core/util/LongBiPredicate.java index cd4ecc45a27..c5ea0c65b46 100644 --- a/src/main/java/net/openhft/chronicle/core/util/LongBiPredicate.java +++ b/src/main/java/net/openhft/chronicle/core/util/LongBiPredicate.java @@ -27,6 +27,7 @@ * * @see Predicate */ +@Deprecated(/*to be removed in x.27*/) @FunctionalInterface public interface LongBiPredicate { diff --git a/src/main/java/net/openhft/chronicle/core/util/LongTriPredicate.java b/src/main/java/net/openhft/chronicle/core/util/LongTriPredicate.java deleted file mode 100644 index f2c16bbf894..00000000000 --- a/src/main/java/net/openhft/chronicle/core/util/LongTriPredicate.java +++ /dev/null @@ -1,97 +0,0 @@ -/* - * Copyright 2016-2022 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.core.util; - -import java.util.Objects; -import java.util.function.Predicate; - -/** - * Represents a predicate (boolean-valued function) of three long arguments. This is - * the three-arity specialization of {@link Predicate}. - * - * @see Predicate - */ -@Deprecated(/* to be removed in x.26 */) -@FunctionalInterface -public interface LongTriPredicate { - - /** - * Evaluates this predicate on the given arguments. - * - * @param t the first input argument - * @param u the second input argument - * @param v the third input argument - * @return {@code true} if the input arguments match the predicate, - * otherwise {@code false} - */ - boolean test(long t, long u, long v); - - /** - * Returns a composed predicate that represents a short-circuiting logical - * AND of this predicate and another. When evaluating the composed - * predicate, if this predicate is {@code false}, then the {@code other} - * predicate is not evaluated. - * - *

Any exceptions thrown during evaluation of either predicate are relayed - * to the caller; if evaluation of this predicate throws an exception, the - * {@code other} predicate will not be evaluated. - * - * @param other a predicate that will be logically-ANDed with this - * predicate - * @return a composed predicate that represents the short-circuiting logical - * AND of this predicate and the {@code other} predicate - * @throws NullPointerException If other is null - */ - default LongTriPredicate and(LongTriPredicate other) { - Objects.requireNonNull(other); - return (long t, long u, long v) -> test(t, u, v) && other.test(t, u, v); - } - - /** - * Returns a predicate that represents the logical negation of this - * predicate. - * - * @return a predicate that represents the logical negation of this - * predicate - */ - default LongTriPredicate negate() { - return (long t, long u, long v) -> !test(t, u, v); - } - - /** - * Returns a composed predicate that represents a short-circuiting logical - * OR of this predicate and another. When evaluating the composed - * predicate, if this predicate is {@code true}, then the {@code other} - * predicate is not evaluated. - * - *

Any exceptions thrown during evaluation of either predicate are relayed - * to the caller; if evaluation of this predicate throws an exception, the - * {@code other} predicate will not be evaluated. - * - * @param other a predicate that will be logically-ORed with this - * predicate - * @return a composed predicate that represents the short-circuiting logical - * OR of this predicate and the {@code other} predicate - * @throws NullPointerException If other is null - */ - default LongTriPredicate or(LongTriPredicate other) { - Objects.requireNonNull(other); - return (long t, long u, long v) -> test(t, u, v) || other.test(t, u, v); - } -} diff --git a/src/main/java/net/openhft/chronicle/core/util/Longs.java b/src/main/java/net/openhft/chronicle/core/util/Longs.java index b9f6c76ba54..5e85547a461 100644 --- a/src/main/java/net/openhft/chronicle/core/util/Longs.java +++ b/src/main/java/net/openhft/chronicle/core/util/Longs.java @@ -19,11 +19,8 @@ package net.openhft.chronicle.core.util; import net.openhft.chronicle.assertions.AssertUtil; -import net.openhft.chronicle.core.internal.invariant.longs.LongBiCondition; import net.openhft.chronicle.core.internal.invariant.longs.LongCondition; -import net.openhft.chronicle.core.internal.invariant.longs.LongTriCondition; -import java.util.function.BooleanSupplier; import java.util.function.Function; import java.util.function.LongPredicate; @@ -57,7 +54,7 @@ private Longs() { */ public static long requireNonNegative(final long value) { if (value < 0) - throw new IllegalArgumentException(failDescription(negative().negate(), value)); + throw new IllegalArgumentException("The provided value (" + value + ") is negative"); return value; } @@ -79,55 +76,10 @@ public static long requireNonNegative(final long value) { */ public static long requirePositive(final long value) { if (value <= 0) - throw new IllegalArgumentException(failDescription(positive(), value)); + throw new IllegalArgumentException("The provided value (" + value + ") is not positive"); return value; } - /** - * Returns the provided {@code value} after checking that it satisfies the provided {@code requirement} throwing - * an {@link IllegalArgumentException} if the check fails. - *

- * This method is designed primarily for doing parameter validation in public methods - * and constructors, as demonstrated below: - *

-     * public Foo(long bar) {
-     *     this.bar = require(nonNegative(), bar);
-     * }
-     * 
- * - * @param requirement to impose on the provided {@code value} - * @param value the value to check - * @return the provided {@code value} if the check passes - * @throws NullPointerException If the provided {@code requirement} is {@code null}. - * @throws IllegalArgumentException If the check fails - */ - @Deprecated(/* to be removed in x.26 */) - public static long require(final LongPredicate requirement, - final long value) { - return require(requirement, value, IllegalArgumentException::new); - } - - /** - * Returns the provided {@code value} after checking that it satisfies the provided {@code requirement} throwing - * an {@link IllegalArgumentException} if the check fails. - *

- * This method is designed primarily for doing parameter validation in public methods - * and constructors, as demonstrated below: - *

-     * public Foo(long bar) {
-     *     this.bar = require(() -> bar > 0, "bar should be positive");
-     * }
-     * 
- * - * @param requirement to impose on the provided {@code value} - * @throws NullPointerException If the provided {@code requirement} is {@code null}. - * @throws IllegalArgumentException If the check fails - */ - @Deprecated(/* to be removed in x.26 */) - public static void require(final BooleanSupplier requirement, final String msg) { - require(requirement, msg, IllegalArgumentException::new); - } - /** * Returns the provided {@code value} after checking that it satisfies the provided {@code requirement} throwing * a custom exception if the check fails. @@ -158,190 +110,6 @@ public static long require(final LongPredicate requ return value; } - /** - * Returns the provided {@code value} after checking that it satisfies the provided {@code requirement} throwing - * a custom exception if the check fails. - *

- * This method is designed primarily for doing parameter validation in public methods - * and constructors, as demonstrated below: - *

-     * public Foo(long bar) {
-     *     this.bar = require(byteConvertible(), bar, ArithmeticException::new);
-     * }
-     * 
- * - * @param Exception typ to throw if the check fails - * @param requirement to impose on the provided {@code value} - * @param exceptionMapper to apply should the check fail - * @throws NullPointerException If the provided {@code requirement} is {@code null} or if the provided - * {@code exceptionMapper} is {@code null}. - * @throws RuntimeException of the specified type of the provided {@code exceptionMapper} - */ - @Deprecated(/* to be removed in x.26 */) - public static void require(final BooleanSupplier requirement, - final String msg, - final Function exceptionMapper) { - requireNonNull(exceptionMapper); - if (!requirement.getAsBoolean()) - throw exceptionMapper.apply(msg); - } - - /** - * Returns the provided {@code value} after checking that it and the provided {@code otherValue} satisfies the - * provided {@code requirement} throwing an {@link IllegalArgumentException} if the check fails. - *

- * Checks that the provided {@code value} and provided {@code otherValue} satisfies the provided {@code requirement}. - *

- * This method is designed primarily for doing parameter validation in public methods - * and constructors, as demonstrated below: - *

-     * public Foo(long bar) {
-     *     this.bar = require(betweenZeroAnd(), bar, 32);
-     * }
-     * 
- * - * @param requirement to impose on the provided {@code value} and {@code otherValue} - * @param value the value to check - * @param otherValue the other value to compare against the provided {@code value} - * @return the provided {@code value} if the check passes - * @throws NullPointerException If the provided {@code requirement} is {@code null}. - * @throws IllegalArgumentException If the check fails - */ - public static long require(final LongBiPredicate requirement, - final long value, - final long otherValue) { - if (!requirement.test(value, otherValue)) - throw new IllegalArgumentException(failDescription(requirement, value, otherValue)); - return value; - } - - /** - * Returns the provided {@code value} after checking that it and the provided {@code otherValue} satisfies the - * provided {@code requirement} throwing an {@link IllegalArgumentException} if the check fails. - *

- * Checks that the provided {@code value} and provided {@code otherValue} satisfies the provided {@code requirement}. - *

- * This method is designed primarily for doing parameter validation in public methods - * and constructors, as demonstrated below: - *

-     * public Foo(long bar) {
-     *     this.bar = require(betweenZeroAnd(), bar, 16, 32, ArithmeticException::new);
-     * }
-     * 
- * - * @param Exception typ to throw if the check fails - * @param requirement to impose on the provided {@code value} and {@code otherValue} - * @param value the value to check - * @param otherValue the other value to compare against the provided {@code value} - * @param exceptionMapper to apply should the check fail - * @return the provided {@code value} if the check passes - * @throws NullPointerException If the provided {@code requirement} is {@code null}. - * @throws IllegalArgumentException If the check fails - */ - @Deprecated(/* to be removed in x.26 */) - public static long require(final LongBiPredicate requirement, - final long value, - final long otherValue, - final Function exceptionMapper) { - requireNonNull(exceptionMapper); - if (!requirement.test(value, otherValue)) - throw exceptionMapper.apply(failDescription(requirement, value, otherValue)); - return value; - } - - /** - * Returns the provided {@code value} after checking that it and the provided {@code otherValue} - * and the provided {@code otherSecondValue} satisfies the provided {@code requirement} - * throwing an {@link IllegalArgumentException} if the check fails. - *

- * This method is designed primarily for doing parameter validation in public methods - * and constructors, as demonstrated below: - *

-     * public Foo(long bar) {
-     *     this.bar = require(between(), bar, 16, 32);
-     * }
-     * 
- * - * @param requirement to impose on the provided values - * @param value the value to check - * @param otherFirstValue the other first value to compare against the provided {@code value} - * @param otherSecondValue the other first value to compare against the provided {@code value} - * @return the provided {@code value} if the check passes - * @throws NullPointerException If the provided {@code requirement} is {@code null}. - * @throws IllegalArgumentException If the check fails - */ - public static long require(final LongTriPredicate requirement, - final long value, - final long otherFirstValue, - final long otherSecondValue) { - return require(requirement, value, otherFirstValue, otherSecondValue, IllegalArgumentException::new); - } - - /** - * Returns the provided {@code value} after checking that it and the provided {@code otherValue} - * and the provided {@code otherSecondValue} satisfies the provided {@code requirement} - * throwing an {@link IllegalArgumentException} if the check fails. - *

- * This method is designed primarily for doing parameter validation in public methods - * and constructors, as demonstrated below: - *

-     * public Foo(long bar) {
-     *     this.bar = require(between(), bar, 16, 32, ArithmeticException::new);
-     * }
-     * 
- * - * @param Exception typ to throw if the check fails - * @param requirement to impose on the provided values - * @param value the value to check - * @param otherFirstValue the other first value to compare against the provided {@code value} - * @param otherSecondValue the other first value to compare against the provided {@code value} - * @param exceptionMapper to apply should the check fail - * @return the provided {@code value} if the check passes - * @throws NullPointerException If the provided {@code requirement} is {@code null}. - * @throws IllegalArgumentException If the check fails - */ - @Deprecated(/* to be removed in x.26 */) - public static long require(final LongTriPredicate requirement, - final long value, - final long otherFirstValue, - final long otherSecondValue, - final Function exceptionMapper) { - requireNonNull(exceptionMapper); - if (!requirement.test(value, otherFirstValue, otherSecondValue)) - throw exceptionMapper.apply(failDescription(requirement, value, otherFirstValue, otherSecondValue)); - return value; - } - - /** - * Asserts that the provided {@code value} satisfies the provided {@code requirement} if - * assertions is enabled (i.e. {@code -ea}) and {@link AssertUtil#SKIP_ASSERTIONS} is {@code false}. - *

- * This method is designed primarily for doing parameter validation in private methods - * and constructors, as demonstrated below: - *

-     * private Foo(long bar) {
-     *     assertIfEnabled(nonNegative, bar);
-     *     this.bar = bar;
-     * }
-     * 
- * - * @param requirement to impose on the provided {@code value} - * @param value the value to check - * @return {@code true} - * @throws NullPointerException If the provided {@code requirement} is {@code null}. There is no guarantee that this - * exception is thrown. For example, if assertions are not enabled, then the exception - * might not be thrown. - * @throws AssertionError if the check fails and assertions are enabled both via the {@code -ea} JVM command - * line option and by setting {@link AssertUtil#SKIP_ASSERTIONS} to {@code false}. - */ - @Deprecated(/* to be removed in x.26 */) - public static boolean assertIfEnabled(final LongPredicate requirement, - final long value) { - assert AssertUtil.SKIP_ASSERTIONS || requirement.test(value) - : failDescription(requirement, value); - return true; - } - /** * Asserts that the provided {@code value} and provided {@code otherValue} satisfies the provided {@code requirement} * if assertions is enabled (i.e. {@code -ea}) and {@link AssertUtil#SKIP_ASSERTIONS} is {@code false}. @@ -373,40 +141,6 @@ public static boolean assertIfEnabled(final LongBiPredicate requirement, return true; } - /** - * Asserts that the provided {@code value}, provided {@code otherFirstValue} and provided {@code otherSecondValue} - * satisfies the provided {@code requirement} if assertions is enabled (i.e. {@code -ea}) and - * {@link AssertUtil#SKIP_ASSERTIONS} is {@code false}. - *

- * This method is designed primarily for doing parameter validation in private methods - * and constructors, as demonstrated below: - *

-     * public private(long bar) {
-     *     assertIfEnabled(between(), bar, 13, 42);
-     *     this.bar = bar;
-     * }
-     * 
- * - * @param requirement to impose on the provided values - * @param value the value to check - * @param otherFirstValue the other first value to compare against the provided {@code value} - * @param otherSecondValue the other first value to compare against the provided {@code value} - * @return {@code true} - * @throws NullPointerException If the provided {@code requirement} is {@code null}. There is no guarantee that this - * exception is thrown. For example, if assertions are not enabled, then the exception - * might not be thrown. - * @throws AssertionError if the check fails and assertions are enabled both via the {@code -ea} JVM command - * line option and by setting {@link AssertUtil#SKIP_ASSERTIONS} to {@code false}. - */ - @Deprecated(/* to be removed in x.26 */) - public static boolean assertIfEnabled(final LongTriPredicate requirement, - final long value, - final long otherFirstValue, - final long otherSecondValue) { - assert AssertUtil.SKIP_ASSERTIONS || requirement.test(value, otherFirstValue, otherSecondValue) - : failDescription(requirement, value, otherFirstValue, otherSecondValue); - return true; - } /** * Returns a human-readable form of a failure message provided that the provided {@code value} did not @@ -418,8 +152,7 @@ public static boolean assertIfEnabled(final LongTriPredicate requirement, * satisfy the provided {@code requirement} * @throws NullPointerException If the provided {@code requirement} is {@code null}. */ - @Deprecated(/* to be removed in x.26 */) - public static String failDescription(final LongPredicate requirement, + private static String failDescription(final LongPredicate requirement, final long value) { return String.format("The provided value (%d) is illegal because it does not satisfy the provided requirement: %d %s", value, value, requirement); } @@ -435,52 +168,12 @@ public static String failDescription(final LongPredicate requirement, * provided {@code otherValue} did not satisfy the provided {@code requirement} * @throws NullPointerException If the provided {@code requirement} is {@code null}. */ - @Deprecated(/* to be removed in x.26 */) - public static String failDescription(final LongBiPredicate requirement, + private static String failDescription(final LongBiPredicate requirement, final long value, final long otherValue) { return String.format("The provided value (%d) is illegal because it does not satisfy the provided requirement: %d %s %d", value, value, requirement, otherValue); } - /** - * Returns a human-readable form of a failure message provided that the provided {@code value}, - * provided {@code otherFirstValue} and provided {@code otherFirstValue} did not satisfy the - * provided {@code requirement}. - * - * @param requirement to imposed on the provided values - * @param value the value to check - * @return a human-readable form of a failure message provided that the provided {@code value} and - * provided {@code otherValue} did not satisfy the provided {@code requirement} - * @throws NullPointerException If the provided {@code requirement} is {@code null}. - */ - @Deprecated(/* to be removed in x.26 */) - public static String failDescription(final LongTriPredicate requirement, - final long value, - final long otherFirstValue, - final long otherSecondValue) { - return String.format("The provided value (%d) is illegal because it does not satisfy the provided requirement: %d %s (%d, %d)", value, value, requirement, otherFirstValue, otherSecondValue); - } - - /** - * Returns a predicate that can test if a value is positive (i.e. value > 0). - * - * @return a predicate that can test if a value is positive (i.e. value > 0) - */ - @Deprecated(/* to be removed in x.26 */) - public static LongPredicate positive() { - return LongCondition.POSITIVE; - } - - /** - * Returns a predicate that can test if a value is negative (i.e. value < 0). - * - * @return a predicate that can test if a value is negative (i.e. value < 0) - */ - @Deprecated(/* to be removed in x.26 */) - public static LongPredicate negative() { - return LongCondition.NEGATIVE; - } - /** * Returns a predicate that can test if a value is non-negative (i.e. value >= 0). *

@@ -491,233 +184,4 @@ public static LongPredicate negative() { public static LongPredicate nonNegative() { return LongCondition.NON_NEGATIVE; } - - /** - * Returns a predicate that can test if a value is zero (i.e. value == 0). - * - * @return a predicate that can test if a value is zero (i.e. value == 0) - */ - @Deprecated(/* to be removed in x.26 */) - public static LongPredicate zero() { - return LongCondition.ZERO; - } - - /** - * Returns a predicate that can test if a value can fit in a {@code byte} (i.e. value ∈ [-128, 127]"). - * - * @return a predicate that can test if a value can fit in a {@code byte} (i.e. value ∈ [-128, 127]") - */ - @Deprecated(/* to be removed in x.26 */) - public static LongPredicate byteConvertible() { - return LongCondition.BYTE_CONVERTIBLE; - } - - /** - * Returns a predicate that can test if a value can fit in a {@code short} (i.e. value ∈ [-32768, 32767]"). - * - * @return a predicate that can test if a value can fit in a {@code short} (i.e. value ∈ [-32768, 32767]") - */ - @Deprecated(/* to be removed in x.26 */) - public static LongPredicate shortConvertible() { - return LongCondition.SHORT_CONVERTIBLE; - } - - /** - * Returns a predicate that can test if a value is an even power of two - * (i.e. log2(value) is an integer). - * - * @return a predicate that can test if a value is an even power of two - * (i.e. log2(value) is an integer) - */ - @Deprecated(/* to be removed in x.26 */) - public static LongPredicate evenPowerOfTwo() { - return LongCondition.EVEN_POWER_OF_TWO; - } - - /** - * Returns a predicate that can test if a value is short aligned - * (i.e. value & (Short.BYTES - 1) == 0). - * - * @return a predicate that can test if a value is short aligned - * (i.e. value & (Short.BYTES - 1) == 0) - */ - @Deprecated(/* to be removed in x.26 */) - public static LongPredicate shortAligned() { - return LongCondition.SHORT_ALIGNED; - } - - /** - * Returns a predicate that can test if a value is int aligned - * (i.e. value & (Integer.BYTES - 1) == 0). - * - * @return a predicate that can test if a value is a int aligned - * (i.e. value & (Integer.BYTES - 1) == 0) - */ - @Deprecated(/* to be removed in x.26 */) - public static LongPredicate intAligned() { - return LongCondition.INT_ALIGNED; - } - - /** - * Returns a predicate that can test if a value is long aligned - * (i.e. value & (Long.BYTES - 1) == 0). - * - * @return a predicate that can test if a value is a long aligned - * (i.e. value & (Long.BYTES - 1) == 0) - */ - @Deprecated(/* to be removed in x.26 */) - public static LongPredicate longAligned() { - return LongCondition.LONG_ALIGNED; - } - - /** - * Returns a predicate that can test if a value is equal to another value. - * - * @return a predicate that can test if a value is equal to another value - */ - @Deprecated(/* to be removed in x.26 */) - public static LongBiPredicate equalTo() { - return LongBiCondition.EQUAL_TO; - } - - /** - * Returns a predicate that can test if a value is greater than to another value. - * - * @return a predicate that can test if a value is greater than to another value - */ - @Deprecated(/* to be removed in x.26 */) - public static LongBiPredicate greaterThan() { - return LongBiCondition.GREATER_THAN; - } - - /** - * Returns a predicate that can test if a value is greater or equal to another value. - * - * @return a predicate that can test if a value is greater or equal to another value - */ - @Deprecated(/* to be removed in x.26 */) - public static LongBiPredicate greaterOrEqual() { - return LongBiCondition.GREATER_OR_EQUAL; - } - - /** - * Returns a predicate that can test if a value is less than to another value. - * - * @return a predicate that can test if a value is less than to another value - */ - @Deprecated(/* to be removed in x.26 */) - public static LongBiPredicate lessThan() { - return LongBiCondition.LESS_THAN; - } - - /** - * Returns a predicate that can test if a value is less or equal to another value. - * - * @return a predicate that can test if a value is less or equal to another value - */ - @Deprecated(/* to be removed in x.26 */) - public static LongBiPredicate lessOrEqual() { - return LongBiCondition.LESS_OR_EQUAL; - } - - /** - * Returns a predicate that can test if a value is between zero and another value (exclusive) - * (i.e value ∈ [0, other value) ). - * - * @return a predicate that can test if a value is between zero and another value (exclusive) - * (i.e value ∈ [0, other value) ) - */ - @Deprecated(/* to be removed in x.26 */) - public static LongBiPredicate betweenZeroAnd() { - return LongBiCondition.BETWEEN_ZERO_AND; - } - - /** - * Returns a predicate that can test if a value is between zero and another value (inclusive) - * (i.e value ∈ [0, other value] ). - * - * @return a predicate that can test if a value is between zero and another value (inclusive) - * (i.e value ∈ [0, other value] ) - */ - @Deprecated(/* to be removed in x.26 */) - public static LongBiPredicate betweenZeroAndClosed() { - return LongBiCondition.BETWEEN_ZERO_AND_CLOSED; - } - - /** - * Returns a predicate that can test if a value is a power of two of another value - * (i.e. value = 2 ^ other value). - * For example, - * {@code powerOfTwo().test(16, 4)} is {@code true} because 2^4 is 16 - * - * @return a predicate that can test if a value is an even power of two - * (i.e. an N exists such that value ^ N is an integer) - */ - @Deprecated(/* to be removed in x.26 */) - public static LongBiPredicate powerOfTwo() { - return LongBiCondition.POWER_OF_TWO; - } - - /** - * Returns a predicate that can test if a value is log2 of another value - * (i.e. value = log2(other value) ). - *

- * For example, - * {@code log2().test(4, 16)} is {@code true} because log2(16) is 4 - * - * @return a predicate that can test if a value is log2 of another value - * (i.e. value = log2(other value) ). - */ - @Deprecated(/* to be removed in x.26 */) - public static LongBiPredicate log2() { - return LongBiCondition.LOG2; - } - - /** - * Returns a predicate that can test if a value is between another first value (inclusive) - * and another second value (exclusive) (i.e value ∈ [other first value , other second value) ). - * - * @return a predicate that can test if a value is between another first value (inclusive) - * and another second value (exclusive) (i.e value ∈ [other first value , other second value) ) - */ - @Deprecated(/* to be removed in x.26 */) - public static LongTriPredicate between() { - return LongTriCondition.BETWEEN; - } - - /** - * Returns a predicate that can test if a value is between (closed) another first value (inclusive) - * and another second value (inclusive) (i.e value ∈ [other first value , other second value] ). - * - * @return a predicate that can test if a value is between (closed) another first value (inclusive) - * and another second value (inclusive) (i.e value ∈ [other first value , other second value] ). - */ - @Deprecated(/* to be removed in x.26 */) - public static LongTriPredicate betweenClosed() { - return LongTriCondition.BETWEEN_CLOSED; - } - - /** - * Returns a predicate that can test if a value is between zero and another first value (inclusive) - * whilst ensuring a value of size defined by another second value can fit. - * (i.e value ∈ [0, other first value - other second value] ). - *

- * This predicate is useful when ensuring that a memory structure can be updated without exceeding its upper - * bounds. For example: - *

-     * public static void putLong(byte[] bytes, long offset, long value) {
-     *         assert nonNull(bytes);
-     *         assert betweenZeroAndReserving.test(offset, bytes.length, Integer.BYTES);
-     *         ...
-     * 
- * - * @return Returns a predicate that can test if a value is between zero and another first value (inclusive) - * whilst ensuring a value of size defined by another second value can fit. - * (i.e value ∈ [0, other first value - other second value] ). - */ - @Deprecated(/* to be removed in x.26 */) - public static LongTriPredicate betweenZeroAndReserving() { - return LongTriCondition.BETWEEN_ZERO_AND_ENSURING; - } - } diff --git a/src/test/java/net/openhft/chronicle/core/UnsafeMemory2Test.java b/src/test/java/net/openhft/chronicle/core/UnsafeMemory2Test.java index bdca4fbaf65..e476f94daa0 100644 --- a/src/test/java/net/openhft/chronicle/core/UnsafeMemory2Test.java +++ b/src/test/java/net/openhft/chronicle/core/UnsafeMemory2Test.java @@ -392,7 +392,9 @@ public void copyMemoryEachWayByteArrayHeapObject() throws NoSuchFieldException { final byte[] data = new byte[Integer.BYTES]; data[0] = 98; MyDTO to = new MyDTO(); + memory.copyMemory(data, 0, to, offset, Integer.BYTES); + assertEquals(data[0], to.num); to.num = 77; memory.copyMemory(to, offset, data, memory.arrayBaseOffset(data.getClass()), Integer.BYTES); diff --git a/src/test/java/net/openhft/chronicle/core/UnsafeMemoryIntTest.java b/src/test/java/net/openhft/chronicle/core/UnsafeMemoryIntTest.java index 9945efe29ed..850788c7a83 100644 --- a/src/test/java/net/openhft/chronicle/core/UnsafeMemoryIntTest.java +++ b/src/test/java/net/openhft/chronicle/core/UnsafeMemoryIntTest.java @@ -36,7 +36,7 @@ public Class type() { @Override public IntPredicate alignedToType() { - return Ints.intAligned(); + return x -> x % 4 == 0; } @Override diff --git a/src/test/java/net/openhft/chronicle/core/UnsafeMemoryLongTest.java b/src/test/java/net/openhft/chronicle/core/UnsafeMemoryLongTest.java index c78cba12309..fd628da705a 100644 --- a/src/test/java/net/openhft/chronicle/core/UnsafeMemoryLongTest.java +++ b/src/test/java/net/openhft/chronicle/core/UnsafeMemoryLongTest.java @@ -36,7 +36,7 @@ public Class type() { @Override public IntPredicate alignedToType() { - return Ints.longAligned(); + return x -> x % 8 == 0; } @Override diff --git a/src/test/java/net/openhft/chronicle/core/UnsafeMemoryShortTest.java b/src/test/java/net/openhft/chronicle/core/UnsafeMemoryShortTest.java index 9e11bc36f37..c1d4c205e68 100644 --- a/src/test/java/net/openhft/chronicle/core/UnsafeMemoryShortTest.java +++ b/src/test/java/net/openhft/chronicle/core/UnsafeMemoryShortTest.java @@ -36,7 +36,7 @@ public Class type() { @Override public IntPredicate alignedToType() { - return Ints.shortAligned(); + return x -> x % 2 == 0; } @Override diff --git a/src/test/java/net/openhft/chronicle/core/benchmarks/CoolerAppendBase10.java b/src/test/java/net/openhft/chronicle/core/benchmarks/CoolerAppendBase10.java deleted file mode 100644 index 25f8d7740a9..00000000000 --- a/src/test/java/net/openhft/chronicle/core/benchmarks/CoolerAppendBase10.java +++ /dev/null @@ -1,46 +0,0 @@ -/* - * Copyright 2016-2022 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.core.benchmarks; - -import net.openhft.chronicle.core.cooler.CoolerTester; -import net.openhft.chronicle.core.cooler.CpuCoolers; -import net.openhft.chronicle.core.io.UnsafeText; - -import static net.openhft.chronicle.core.UnsafeMemory.UNSAFE; - -public class CoolerAppendBase10 { - - static long blackhole; - - public static void main(String[] args) { - long address = UNSAFE.allocateMemory(32); - new CoolerTester(CpuCoolers.PAUSE1, CpuCoolers.BUSY100) -// .add("noop", () -> null) - .add("20d", () -> { - blackhole = UnsafeText.appendFixed(address, -Integer.MAX_VALUE); - return null; - }) - .runTimeMS(10000) - .repeat(6) - .run(); - - UNSAFE.freeMemory(address); - } - -} diff --git a/src/test/java/net/openhft/chronicle/core/io/UnsafeTextTest.java b/src/test/java/net/openhft/chronicle/core/io/UnsafeTextTest.java deleted file mode 100644 index 10bd2425208..00000000000 --- a/src/test/java/net/openhft/chronicle/core/io/UnsafeTextTest.java +++ /dev/null @@ -1,232 +0,0 @@ -/* - * Copyright 2016-2022 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.core.io; - -import net.openhft.chronicle.core.CoreTestCommon; -import net.openhft.chronicle.core.cooler.CoolerTester; -import net.openhft.chronicle.core.cooler.CpuCoolers; -import org.junit.Before; -import org.junit.Test; - -import java.util.Random; -import java.util.stream.IntStream; -import java.util.stream.LongStream; - -import static net.openhft.chronicle.core.UnsafeMemory.UNSAFE; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.fail; - -public class UnsafeTextTest extends CoreTestCommon { - - static long blackhole; - - @Override - @Before - public void threadDump() { - super.threadDump(); - } - - @Test - public void coolerAppendBase10quick() { - long address = UNSAFE.allocateMemory(32); - - try { - - new CoolerTester(CpuCoolers.PAUSE1, CpuCoolers.BUSY1) -// .add("noop", () -> null) - .add("20d", () -> { - blackhole = UnsafeText.appendFixed(address, -Integer.MAX_VALUE); - return null; - }) - .runTimeMS(100) - .repeat(3) - .run(); - - final String memVal = LongStream.range(address, blackhole) - .mapToInt(addr -> UNSAFE.getByte(null, addr)) - .mapToObj(c -> (char) c) - .reduce(new StringBuilder(), StringBuilder::append, StringBuilder::append) - .toString(); - - assertEquals(Long.toString(-Integer.MAX_VALUE), memVal); - } finally { - UNSAFE.freeMemory(address); - } - - } - - @Test - public void testAppendDouble() { - // TODO FIX - // Examples for https://github.com/OpenHFT/Chronicle-Core/issues/493 - testAppendDoubleOnce(5.959231521092378E-8, "5.959231521092378E-8"); - testAppendDoubleOnce(5.954710747053357E-8, "5.954710747053357E-8"); - testAppendDoubleOnce(-4.3723721608241563E-8, "-4.3723721608241563E-8"); - testAppendDoubleOnce(3.5645738448792343E-8, "3.5645738448792343E-8"); - testAppendDoubleOnce(1.1914579369762811E-8, "1.1914579369762811E-8"); - - // FIXED - testAppendDoubleOnce(-1.4778838950354771E-9, "-1.4778838950354771E-9"); - testAppendDoubleOnce(-145344868913.80003, "-145344868913.80003"); - - testAppendDoubleOnce(1.4753448053710411E-8, "1.4753448053710411E-8"); - testAppendDoubleOnce(4.731428525883379E-10, "4.731428525883379E-10"); - testAppendDoubleOnce(1e-5, "0.00001"); - - testAppendDoubleOnce(5.7270847085938394E-9, "5.7270847085938394E-9"); - testAppendDoubleOnce(-3.5627763205104632E-9, "-3.5627763205104632E-9"); - testAppendDoubleOnce(3.4363211797092447E-10, "3.4363211797092447E-10"); - - testAppendDoubleOnce(0.91234567890123456789, "0.9123456789012345"); - testAppendDoubleOnce(0.7205789375929972, "0.7205789375929972"); - testAppendDoubleOnce(1.7205789375929972E-8, "1.7205789375929972E-8"); - testAppendDoubleOnce(1.000000459754255, "1.000000459754255"); -// testAppendDoubleOnce(1.0000004597542552, "1.0000004597542552"); - testAppendDoubleOnce(-0.0042633243189823394, "-0.0042633243189823394"); - // too high - testAppendDoubleOnce(4.3634067645459027E-4, "0.00043634067645459027"); - testAppendDoubleOnce(-4.8378951079402273E-4, "-0.00048378951079402273"); - testAppendDoubleOnce(3.8098893793449994E-4, "0.00038098893793449994"); - testAppendDoubleOnce(-0.0036980489197619678, "-0.0036980489197619678"); - - // FIXED - testAppendDoubleOnce(1.1777536373898703E-7, "0.00000011777536373898703"); - testAppendDoubleOnce(8.577881719106565E-8, "0.00000008577881719106565"); - testAppendDoubleOnce(1.1709707236415293E-7, "0.00000011709707236415293"); - testAppendDoubleOnce(1.0272238286878982E-7, "0.00000010272238286878982"); - testAppendDoubleOnce(9.077547054210796E-8, "0.00000009077547054210796"); - testAppendDoubleOnce(-1.1914407211387385E-7, "-0.00000011914407211387385"); -// testAppendDoubleOnce(1.0626477603237785E-10, "0.00000000010626477603237785"); - testAppendDoubleOnce(8.871684275243539E-4, "0.0008871684275243539"); - testAppendDoubleOnce(8.807878708605213E-4, "0.0008807878708605213"); - testAppendDoubleOnce(8.417670165790972E-4, "0.0008417670165790972"); - testAppendDoubleOnce(0.0013292726996348332, "0.0013292726996348332"); - testAppendDoubleOnce(2.4192540417349368E-4, "0.00024192540417349368"); - testAppendDoubleOnce(1.9283711356548258E-4, "0.00019283711356548258"); - testAppendDoubleOnce(-8.299137873077923E-5, "-0.00008299137873077923"); - - // OK - testAppendDoubleOnce(0, "0.0"); - testAppendDoubleOnce(0.001, "0.001"); - testAppendDoubleOnce(0.0001, "0.0001"); - testAppendDoubleOnce(0.000001, "0.000001"); - testAppendDoubleOnce(0.0000001, "0.0000001"); - testAppendDoubleOnce(1.0E-8, "1.0E-8"); - testAppendDoubleOnce(1.0E-9, "1.0E-9"); - testAppendDoubleOnce(0.009, "0.009"); - testAppendDoubleOnce(0.0009, "0.0009"); - testAppendDoubleOnce(0.00009, "0.00009"); - testAppendDoubleOnce(0.000009, "0.000009"); - testAppendDoubleOnce(0.0000009, "0.0000009"); - testAppendDoubleOnce(0.00000009, "0.00000009"); - testAppendDoubleOnce(9.0E-9, "9.0E-9"); - testAppendDoubleOnce(Double.NaN, "NaN"); - testAppendDoubleOnce(Double.POSITIVE_INFINITY, "Infinity"); - testAppendDoubleOnce(Double.NEGATIVE_INFINITY, "-Infinity"); - testAppendDoubleOnce(0.1, "0.1"); - testAppendDoubleOnce(12.0, "12.0"); - testAppendDoubleOnce(12.1, "12.1"); - testAppendDoubleOnce(12.00000001, "12.00000001"); - testAppendDoubleOnce(1e-9 + Math.ulp(1e-9), "1.0000000000000003E-9"); - testAppendDoubleOnce(1e-10 + Math.ulp(1e-10), "1.0000000000000002E-10"); - testAppendDoubleOnce(1e-11 + Math.ulp(1e-11), "1.0000000000000001E-11"); - double d = -1e30; - testAppendDoubleOnce(d - Math.ulp(d), "-1000000000000000158000000000000"); - d = -1e31; - testAppendDoubleOnce(d - Math.ulp(d), "-1.0000000000000001E31"); - d = -1e32; - testAppendDoubleOnce(d - Math.ulp(d), "-1.0000000000000002E32"); - - } - - public void testAppendDoubleOnce(double value, String expectedValue) { - long address = UNSAFE.allocateMemory(max + 8); - try { - final String memVal = appendDoubleToString(value, address); - assertEquals("value; " + value, expectedValue, memVal); - } finally { - UNSAFE.freeMemory(address); - } - } - - static final int max = 32; - - @Test - public void testRandom() { - int runLength = 10_000; - IntStream.range(0, runLength).parallel().forEach(t -> { - Random r = new Random(); - long address = UNSAFE.allocateMemory(max + 8); - long l = r.nextLong() | 1L; - for (int i = 0; i < 1_000; i++) { - // agitate - l += 2; - double d = Double.longBitsToDouble(l); - if (!Double.isFinite(d) || Math.abs(d) < 1e-20 || Math.abs(d) > 5e11) { - break; - } - String s = appendDoubleToString(d, address); - double d2 = Double.parseDouble(s); - if (d != d2) { - String message = "" + (d - d2); - assertEquals(message, d, d2, 0); - } - } - // this is called unless the test is about to die - UNSAFE.freeMemory(address); - }); - } - - @Test - public void testSequential() { - IntStream.range(0, 300).parallel().forEach(t -> { - // odd numbers have the most precision error - long address = UNSAFE.allocateMemory(max + 8); - // double only has 16-17 digits of accuracy so 1 + x/1e15 has 16 digits. - double n = 1e15; - for (int i = 1; i < 10_000; i += 2) { - // multiply by 10191 to traverse the space. - long l = (t * 10_000L + i) * 10191L + (long) n; - double d = l / n; - String s = appendDoubleToString(d, address); - double d2 = Double.parseDouble(s); - if (d != d2) - assertEquals("" + (d - d2), d, d2, 0); - } - // this is called unless the test is about to die - UNSAFE.freeMemory(address); - }); - } - - public String appendDoubleToString(double value, long address) { - UNSAFE.putLong(address + max, 0L); - final long endAddress = UnsafeText.appendDouble(address, value); - if (endAddress > address + max) - fail("value: " + value + " length: " + (endAddress - address)); - long end = UNSAFE.getLong(address + max); - if (end != 0L) - fail("Overwrite: " + Long.toHexString(end)); - return LongStream.range(address, endAddress) - .mapToInt(addr -> UNSAFE.getByte(null, addr)) - .mapToObj(c -> (char) c) - .reduce(new StringBuilder(), StringBuilder::append, StringBuilder::append) - .toString(); - } - -} diff --git a/src/test/java/net/openhft/chronicle/core/threads/JitterSamplerTest.java b/src/test/java/net/openhft/chronicle/core/threads/JitterSamplerTest.java deleted file mode 100644 index 914ce8b3ce7..00000000000 --- a/src/test/java/net/openhft/chronicle/core/threads/JitterSamplerTest.java +++ /dev/null @@ -1,71 +0,0 @@ -/* - * Copyright 2016-2022 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.core.threads; - -import net.openhft.chronicle.core.CoreTestCommon; -import net.openhft.chronicle.core.Jvm; -import org.junit.Test; - -import java.util.concurrent.CountDownLatch; -import java.util.concurrent.TimeUnit; - -import static org.junit.Assert.*; - -public class JitterSamplerTest extends CoreTestCommon { - - @Test - public void takeSnapshot() throws InterruptedException { - CountDownLatch latch = new CountDownLatch(2); - Thread t = new Thread(() -> { - JitterSampler.atStage("started"); - waitForLatch(latch); - int millis = Jvm.isArm() ? 120 : 60; - Jvm.pause(millis); - JitterSampler.atStage("finishing"); - Jvm.pause(millis); - JitterSampler.finished(); - }); - t.start(); - waitForLatch(latch); - for (int i = 0; i < 10; i++) { - Jvm.busyWaitMicros(1000); - String s = JitterSampler.takeSnapshot(10_000_000); - final String desc = JitterSampler.desc; - if ("finishing".equals(desc)) { - if (s != null && s.contains("finish")) - break; - } else { - assertEquals("started", desc); - } - } - t.join(); - String s = JitterSampler.takeSnapshot(); - assertNull(s); - } - - private void waitForLatch(CountDownLatch latch) { - latch.countDown(); - try { - assertTrue(latch.await(3, TimeUnit.SECONDS)); - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); - throw new RuntimeException("Interrupted waiting for latch", e); - } - } -} diff --git a/src/test/java/net/openhft/chronicle/core/util/IntsTest.java b/src/test/java/net/openhft/chronicle/core/util/IntsTest.java deleted file mode 100644 index 4a7f25de546..00000000000 --- a/src/test/java/net/openhft/chronicle/core/util/IntsTest.java +++ /dev/null @@ -1,92 +0,0 @@ -/* - * Copyright 2016-2022 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.core.util; - -import net.openhft.chronicle.core.CoreTestCommon; -import org.jetbrains.annotations.NotNull; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.fail; - -public class IntsTest extends CoreTestCommon { - - @Test - public void require1Arg() { - test(1, 0, v -> Ints.require(Ints.positive(), v)); - test(1, -1, v -> Ints.require(Ints.positive(), v)); - } - - @Test - public void require2Arg() { - test(1, 0, v -> Ints.require(Ints.equalTo(), v, 1)); - } - - @Test - public void require3Ard() { - test(0, 16, v -> Ints.require(Ints.between(), v, 0, 16)); - test(15, -1, v -> Ints.require(Ints.between(), v, 0, 16)); - } - - @Test - public void shortAligned() { - test(0, 1, v -> Ints.require(Ints.shortAligned(), v)); - test(2, 3, v -> Ints.require(Ints.shortAligned(), v)); - test(4, 5, v -> Ints.require(Ints.shortAligned(), v)); - test(6, 7, v -> Ints.require(Ints.shortAligned(), v)); - test(8, 9, v -> Ints.require(Ints.shortAligned(), v)); - } - @Test - public void intAligned() { - test(0, 1, v -> Ints.require(Ints.intAligned(), v)); - test(0, 2, v -> Ints.require(Ints.intAligned(), v)); - test(0, 3, v -> Ints.require(Ints.intAligned(), v)); - test(4, 5, v -> Ints.require(Ints.intAligned(), v)); - test(4, 6, v -> Ints.require(Ints.intAligned(), v)); - } - @Test - public void longAligned() { - test(0, 1, v -> Ints.require(Ints.longAligned(), v)); - test(0, 2, v -> Ints.require(Ints.longAligned(), v)); - test(0, 3, v -> Ints.require(Ints.longAligned(), v)); - test(0, 4, v -> Ints.require(Ints.longAligned(), v)); - test(0, 5, v -> Ints.require(Ints.longAligned(), v)); - test(0, 6, v -> Ints.require(Ints.longAligned(), v)); - test(0, 7, v -> Ints.require(Ints.longAligned(), v)); - test(8, 9, v -> Ints.require(Ints.longAligned(), v)); - } - - private void test(final int happy, - final int sad, - @NotNull final IntUnaryOperator mapper) { - final long result = mapper.applyAsInt(happy); - assertEquals(happy, result); - try { - final long result2 = mapper.applyAsInt(sad); - fail(result2 + " is not valid!"); - } catch (IllegalArgumentException ignored) { - // Happy path - } - } - - interface IntUnaryOperator { - long applyAsInt(int happy) throws IllegalArgumentException; - } - -} diff --git a/src/test/java/net/openhft/chronicle/core/util/LongsTest.java b/src/test/java/net/openhft/chronicle/core/util/LongsTest.java deleted file mode 100644 index 1e42ba9bdfa..00000000000 --- a/src/test/java/net/openhft/chronicle/core/util/LongsTest.java +++ /dev/null @@ -1,99 +0,0 @@ -/* - * Copyright 2016-2022 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.core.util; - -import net.openhft.chronicle.core.CoreTestCommon; -import org.jetbrains.annotations.NotNull; -import org.junit.Test; - -import static org.junit.Assert.*; - -public class LongsTest extends CoreTestCommon { - - @Test - public void requireBoolean() { - assertThrows(IllegalArgumentException.class, () -> Longs.require(() -> false, "Error")); - Longs.require(() -> true, "OK"); - } - - @Test - public void require1Arg() { - test(1, 0, v -> Longs.require(Longs.positive(), v)); - test(1, -1, v -> Longs.require(Longs.positive(), v)); - } - - @Test - public void require2Arg() { - test(1, 0, v -> Longs.require(Longs.equalTo(), v, 1)); - } - - @Test - public void require3Ard() { - test(0, 16, v -> Longs.require(Longs.between(), v, 0, 16)); - test(15, -1, v -> Longs.require(Longs.between(), v, 0, 16)); - } - - @Test - public void shortAligned() { - test(0, 1, v -> Longs.require(Longs.shortAligned(), v)); - test(2, 3, v -> Longs.require(Longs.shortAligned(), v)); - test(4, 5, v -> Longs.require(Longs.shortAligned(), v)); - test(6, 7, v -> Longs.require(Longs.shortAligned(), v)); - test(8, 9, v -> Longs.require(Longs.shortAligned(), v)); - } - - @Test - public void intAligned() { - test(0, 1, v -> Longs.require(Longs.intAligned(), v)); - test(0, 2, v -> Longs.require(Longs.intAligned(), v)); - test(0, 3, v -> Longs.require(Longs.intAligned(), v)); - test(4, 5, v -> Longs.require(Longs.intAligned(), v)); - test(4, 6, v -> Longs.require(Longs.intAligned(), v)); - } - - @Test - public void longAligned() { - test(0, 1, v -> Longs.require(Longs.longAligned(), v)); - test(0, 2, v -> Longs.require(Longs.longAligned(), v)); - test(0, 3, v -> Longs.require(Longs.longAligned(), v)); - test(0, 4, v -> Longs.require(Longs.longAligned(), v)); - test(0, 5, v -> Longs.require(Longs.longAligned(), v)); - test(0, 6, v -> Longs.require(Longs.longAligned(), v)); - test(0, 7, v -> Longs.require(Longs.longAligned(), v)); - test(8, 9, v -> Longs.require(Longs.longAligned(), v)); - } - - private void test(final int happy, - final int sad, - @NotNull final LongUnaryOperator mapper) { - final long result = mapper.applyAsLong(happy); - assertEquals(happy, result); - try { - final long result2 = mapper.applyAsLong(sad); - fail(result2 + " is not valid!"); - } catch (IllegalArgumentException ignored) { - // Happy path - } - } - - interface LongUnaryOperator { - long applyAsLong(int happy) throws IllegalArgumentException; - } - -}