1 /** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one or more 4 * contributor license agreements. See the NOTICE file distributed with 5 * this work for additional information regarding copyright ownership. 6 * The ASF licenses this file to You under the Apache License, Version 2.0 7 * (the "License"); you may not use this file except in compliance with 8 * the License. You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 */ 18 package org.apache.xbean.recipe; 19 20 /** 21 * Reference is a named (lazy) reference from one object to another. This data class is updated when the reference 22 * is resolved which can be immedately when the ref is created, or later when an instance with the referenced 23 * name is created. 24 * <p/> 25 * When the reference is resolved, an optional Action will be invoked which is commonly used to update a 26 * property on the source object of the reference. 27 */ 28 public class Reference { 29 private final String name; 30 private boolean resolved; 31 private Object instance; 32 private Action action; 33 34 /** 35 * Create a reference to the specified name. 36 * @param name the name of the referenced object 37 */ 38 public Reference(String name) { 39 this.name = name; 40 } 41 42 /** 43 * Gets the name of the referenced object. 44 * @return name the name of the referenced object 45 */ 46 public String getName() { 47 return name; 48 } 49 50 /** 51 * Has this reference been resolved? 52 * @return true if the reference has been resolved; false otherwise 53 */ 54 public boolean isResolved() { 55 return resolved; 56 } 57 58 /** 59 * Gets the referenced object instance or null if the reference has not been resolved yet; 60 * 61 * @return the referenced object instance or null 62 */ 63 public Object get() { 64 return instance; 65 } 66 67 /** 68 * Sets the referenced object instance. If an action is registered the onSet method is invoked. 69 * 70 * @param object the reference instance 71 */ 72 public void set(Object object) { 73 if (resolved) { 74 throw new ConstructionException("Reference has already been resolved"); 75 } 76 resolved = true; 77 this.instance = object; 78 if (action != null) { 79 action.onSet(this); 80 } 81 } 82 83 /** 84 * Registers an action to invoke when the instance is set. If the instance, has already been set, the 85 * onSet method will immedately be invoked. 86 * 87 * @return the action to invoke when this refernce is resolved; not null 88 */ 89 public void setAction(Action action) { 90 if (action == null) { 91 throw new NullPointerException("action is null"); 92 } 93 this.action = action; 94 if (resolved) { 95 action.onSet(this); 96 } 97 } 98 99 public static interface Action { 100 void onSet(Reference ref); 101 } 102 }