- add third_party src.
[platform/framework/web/crosswalk.git] / src / third_party / cacheinvalidation / src / java / com / google / ipc / invalidation / util / Box.java
1 /*
2  * Copyright 2011 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package com.google.ipc.invalidation.util;
18
19 import com.google.common.base.Supplier;
20
21 /**
22  * Container for a single arbitrary value. Useful when a nested callback needs
23  * to modify a primitive type, which is ordinarily not possible as variables
24  * available to nested callbacks need to be declared final.
25  *
26  * @param <T> Type of the value being boxed.
27  *
28  */
29 public class Box<T> implements Supplier<T> {
30
31   /** Contents of the box. */
32   private T value;
33
34   /** Constructs a box with the given initial {@code value}. */
35   public Box(T value) {
36     this.value = value;
37   }
38
39   /**
40    * Returns a supplier for the given value. Note that such a getter's internal value cannot be
41    * changed (by definition).
42    */
43   public static <T> Supplier<T> createSupplier(final T value) {
44     return Box.of(value);
45   }
46
47   /** Constructs a Box with {@code null} as the value. */
48   public Box() {
49     this.value = null;
50   }
51
52   /** Constructs and returns a {@code Box} that wraps {@code objectValue}. */
53   public static <T> Box<T> of(T objectValue) {
54     return new Box<T>(objectValue);
55   }
56
57   public void set(T objectValue) {
58     this.value = objectValue;
59   }
60
61   @Override
62   public T get() {
63     return value;
64   }
65
66   @Override
67   public String toString() {
68     return (value == null) ? null : value.toString();
69   }
70 }