001/* 002 * Trident - A Multithreaded Server Alternative 003 * Copyright 2014 The TridentSDK Team 004 * 005 * Licensed under the Apache License, Version 2.0 (the "License"); 006 * you may not use this file except in compliance with the License. 007 * You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package net.tridentsdk.util; 018 019import javax.annotation.concurrent.ThreadSafe; 020 021/** 022 * Holds a value to cheat inner class finalization requirements 023 * 024 * @author The TridentSDK Team 025 */ 026@ThreadSafe 027public final class Value<T> { 028 private volatile T value; 029 030 private Value(T t) { 031 this.value = t; 032 } 033 034 /** 035 * Creates a new value which has the initial state set to the provided object 036 * 037 * @param t the value to hold 038 * @param <T> the value type 039 * @return the new value holder 040 */ 041 public static <T> Value<T> of(T t) { 042 return new Value<>(t); 043 } 044 045 /** 046 * Creates a new null value 047 * 048 * @param <T> the value type 049 * @return the null value 050 */ 051 public static <T> Value<T> none() { 052 return new Value<>(null); 053 } 054 055 /** 056 * Sets the value 057 * 058 * @param t the value 059 * @return the set value 060 */ 061 public T set(T t) { 062 this.value = t; 063 return t; 064 } 065 066 /** 067 * Gets the value 068 * 069 * @return the value 070 */ 071 public T get() { 072 return this.value; 073 } 074}