com.simiacryptus.mindseye.lang.Singleton Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of mindseye-core Show documentation
Show all versions of mindseye-core Show documentation
Core Neural Networks Framework
/*
* Copyright (c) 2018 by Andrew Charneski.
*
* The author licenses this file to you 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 com.simiacryptus.mindseye.lang;
import javax.annotation.Nonnull;
import java.util.concurrent.BlockingDeque;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.function.Supplier;
/**
* An asynchronous, settable reference buffer. Allows consumers to block until a value is availble.
*
* @param the type parameter
*/
public class Singleton implements Supplier {
private final BlockingDeque deque = new LinkedBlockingDeque<>();
/**
* Instantiates a new Singleton.
*/
public Singleton() {
}
@Override
public T get() {
try {
T take = deque.take();
deque.add(take);
return take;
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
/**
* Set.
*
* @param obj the obj
* @return the singleton
*/
@Nonnull
public Singleton set(T obj) {
assert deque.isEmpty();
deque.add(obj);
return this;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy