001package jmri;
002
003
004import javax.annotation.CheckReturnValue;
005import javax.annotation.Nonnull;
006
007/**
008 * A reference to an object where the object must not be null.
009 * It's a faster replacement for AtomicReference when thread safety is not
010 * needed.
011 *
012 * @param <E> the type of the reference
013 *
014 * @author Daniel Bergqvist Copyright (C) 2024
015 */
016public class ReferenceNotNull<E> {
017
018    private E _ref;
019
020    /**
021     * Create an instance of Reference.
022     */
023    public ReferenceNotNull() {
024    }
025
026    /**
027     * Create an instance of Reference.
028     * @param ref the reference
029     */
030    public ReferenceNotNull(@Nonnull E ref) {
031        this._ref = ref;
032    }
033
034    /**
035     * Set the reference.
036     * @param ref the new reference
037     */
038    public void set(@Nonnull E ref) {
039        this._ref = ref;
040    }
041
042    /**
043     * Return the reference.
044     * @return the reference
045     */
046    @CheckReturnValue
047    @Nonnull
048    public E get() {
049        return _ref;
050    }
051
052}