2 * Copyright 2011 Google Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.google.ipc.invalidation.util;
19 import com.google.common.base.Supplier;
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.
26 * @param <T> Type of the value being boxed.
29 public class Box<T> implements Supplier<T> {
31 /** Contents of the box. */
34 /** Constructs a box with the given initial {@code value}. */
40 * Returns a supplier for the given value. Note that such a getter's internal value cannot be
41 * changed (by definition).
43 public static <T> Supplier<T> createSupplier(final T value) {
47 /** Constructs a Box with {@code null} as the value. */
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);
57 public void set(T objectValue) {
58 this.value = objectValue;
67 public String toString() {
68 return (value == null) ? null : value.toString();