View Javadoc

1   /*
2    * Copyright 2010 FatWire Corporation. All Rights Reserved.
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  package com.fatwire.gst.foundation.tagging;
17  
18  /**
19   * Utilities for converting a tag into a cache dependency string and back
20   * 
21   * @author Tony Field
22   * @since Jul 28, 2010
23   */
24  public final class TagUtils {
25  
26      private static final String IITEM_PREFIX = "gsf-tag-";
27  
28      /**
29       * Return the cache dependency string format corresponding to the specified
30       * tag
31       * 
32       * @param tag the tag
33       * @return string
34       */
35      public static String convertTagToCacheDepString(Tag tag) {
36          return IITEM_PREFIX + tag.getTag();
37      }
38  
39      /**
40       * Convert the cache dep string to a Tag. If the string is not valid, null
41       * is returned
42       * 
43       * @param cacheDepString input string, note not the same as the tag itself.
44       * @return Tag or null
45       * @see #convertTagToCacheDepString
46       */
47      public static Tag convertCacheDepStringToTag(String cacheDepString) {
48          if (cacheDepString != null && cacheDepString.length() > IITEM_PREFIX.length()
49                  && cacheDepString.startsWith(IITEM_PREFIX)) {
50              return asTag(cacheDepString.substring(IITEM_PREFIX.length() + 1));
51          }
52          return null;
53      }
54  
55      public static Tag asTag(final String tagValue) {
56          return new Tag() {
57              public String getTag() {
58                  return tagValue;
59              }
60  
61              public String toString() {
62                  return "tag:" + getTag();
63              }
64  
65              @Override
66              public int hashCode() {
67                  return tagValue.hashCode();
68              }
69  
70              @Override
71              public boolean equals(Object o) {
72                  if (o instanceof Tag) {
73                      Tag t = (Tag) o;
74                      return t.getTag().equals(tagValue);
75                  }
76                  return false;
77              }
78  
79          };
80      }
81  }