View Javadoc

1   package org.kuali.rice.core.api.cache;
2   
3   import org.apache.commons.lang.StringUtils;
4   import org.kuali.rice.core.api.CoreConstants;
5   import org.kuali.rice.core.api.mo.AbstractDataTransferObject;
6   import org.w3c.dom.Element;
7   
8   import javax.xml.bind.annotation.XmlAccessType;
9   import javax.xml.bind.annotation.XmlAccessorType;
10  import javax.xml.bind.annotation.XmlAnyElement;
11  import javax.xml.bind.annotation.XmlElement;
12  import javax.xml.bind.annotation.XmlRootElement;
13  import javax.xml.bind.annotation.XmlType;
14  import java.util.Collection;
15  
16  @XmlRootElement(name = CacheTarget.Constants.ROOT_ELEMENT_NAME)
17  @XmlAccessorType(XmlAccessType.NONE)
18  @XmlType(name = CacheTarget.Constants.TYPE_NAME, propOrder = {
19          CacheTarget.Elements.CACHE,
20          CacheTarget.Elements.KEY,
21          CoreConstants.CommonElements.FUTURE_ELEMENTS
22  })
23  /**
24   * A class that represents a target of a cache operation.  If the cache key
25   * is not specified then the entire cache is the target.
26   */
27  public final class CacheTarget extends AbstractDataTransferObject {
28  
29      @XmlElement(name = Elements.CACHE, required = true)
30      private final String cache;
31      @XmlElement(name = Elements.KEY, required = false)
32      private final String key;
33      @SuppressWarnings("unused")
34      @XmlAnyElement
35      private final Collection<Element> _futureElements = null;
36  
37      /**
38       * This constructor should never be called except during JAXB unmarshalling.
39       */
40      private CacheTarget() {
41          this.cache = null;
42          this.key = null;
43      }
44  
45      private CacheTarget(String cache, String key) {
46          if (StringUtils.isBlank(cache)) {
47              throw new IllegalArgumentException("cache is blank or null");
48          }
49  
50          this.cache = cache;
51          this.key = key;
52      }
53  
54      /**
55       * Creates an instance targeting a entire cache.
56       * @param cache the name of the cache.  cannot be a null of blank string.
57       * @return an instance
58       * @throws IllegalArgumentException if the cache is null or blank
59       */
60      public static CacheTarget entireCache(String cache) {
61          return new CacheTarget(cache, null);
62      }
63  
64      /**
65       * Creates an instance targeting a single item in a cache.
66       * @param cache The name of the cache.  cannot be a null of blank string.
67       * @param key The key of the item in the cache.  cannot be a null of blank string.
68       * @return an instance
69       * @throws IllegalArgumentException if the cache or key is null or blank
70       */
71      public static CacheTarget singleEntry(String cache, String key) {
72          if (StringUtils.isBlank(key)) {
73              throw new IllegalArgumentException("a blank or null key does not target a single entry");
74          }
75  
76          return new CacheTarget(cache, key);
77      }
78  
79      /**
80       * Checks if an instance contains a key.
81       * @return true if a key exists
82       */
83      public boolean containsKey() {
84          return key != null;
85      }
86  
87      /**
88       * The name of the cache to target.  will not return a null of blank string.
89       * @return the name.
90       */
91      public String getCache() {
92          return cache;
93      }
94  
95      /**
96       * The key of an item in the cache.  optional. can return null or blank string.
97       * @return the key
98       */
99      public String getKey() {
100         return key;
101     }
102 
103     /**
104      * Defines some internal constants used on this class.
105      */
106     static class Constants {
107         final static String ROOT_ELEMENT_NAME = "cacheTarget";
108         final static String TYPE_NAME = "CacheTargetType";
109     }
110 
111     /**
112      * A private class which exposes constants which define the XML element names to use
113      * when this object is marshalled to XML.
114      */
115     static class Elements {
116         final static String CACHE = "cache";
117         final static String KEY = "key";
118     }
119 }