X-Git-Url: https://git.mdrn.pl/pylucene.git/blobdiff_plain/a2e61f0c04805cfcb8706176758d1283c7e3a55c..aaeed5504b982cf3545252ab528713250aa33eed:/lucene-java-3.5.0/lucene/src/java/org/apache/lucene/util/SetOnce.java diff --git a/lucene-java-3.5.0/lucene/src/java/org/apache/lucene/util/SetOnce.java b/lucene-java-3.5.0/lucene/src/java/org/apache/lucene/util/SetOnce.java new file mode 100644 index 0000000..f280ee7 --- /dev/null +++ b/lucene-java-3.5.0/lucene/src/java/org/apache/lucene/util/SetOnce.java @@ -0,0 +1,77 @@ +package org.apache.lucene.util; + +import java.util.concurrent.atomic.AtomicBoolean; + +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF 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. + */ + +/** + * A convenient class which offers a semi-immutable object wrapper + * implementation which allows one to set the value of an object exactly once, + * and retrieve it many times. If {@link #set(Object)} is called more than once, + * {@link AlreadySetException} is thrown and the operation + * will fail. + * + * @lucene.experimental + */ +public final class SetOnce { + + /** Thrown when {@link SetOnce#set(Object)} is called more than once. */ + public static final class AlreadySetException extends RuntimeException { + public AlreadySetException() { + super("The object cannot be set twice!"); + } + } + + private volatile T obj = null; + private final AtomicBoolean set; + + /** + * A default constructor which does not set the internal object, and allows + * setting it by calling {@link #set(Object)}. + */ + public SetOnce() { + set = new AtomicBoolean(false); + } + + /** + * Creates a new instance with the internal object set to the given object. + * Note that any calls to {@link #set(Object)} afterwards will result in + * {@link AlreadySetException} + * + * @throws AlreadySetException if called more than once + * @see #set(Object) + */ + public SetOnce(T obj) { + this.obj = obj; + set = new AtomicBoolean(true); + } + + /** Sets the given object. If the object has already been set, an exception is thrown. */ + public final void set(T obj) { + if (set.compareAndSet(false, true)) { + this.obj = obj; + } else { + throw new AlreadySetException(); + } + } + + /** Returns the object set by {@link #set(Object)}. */ + public final T get() { + return obj; + } +}