1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet.reverendfun.util;
21  
22  import com.liferay.portal.kernel.util.StringComparator;
23  import com.liferay.portal.kernel.util.StringPool;
24  import com.liferay.portal.kernel.util.StringUtil;
25  import com.liferay.portal.kernel.webcache.WebCacheItem;
26  import com.liferay.portal.kernel.webcache.WebCachePoolUtil;
27  import com.liferay.portal.util.ContentUtil;
28  
29  import java.util.ArrayList;
30  import java.util.Collections;
31  import java.util.HashSet;
32  import java.util.Iterator;
33  import java.util.List;
34  import java.util.Set;
35  
36  /**
37   * <a href="ReverendFunUtil.java.html"><b><i>View Source</i></b></a>
38   *
39   * @author Brian Wing Shun Chan
40   *
41   */
42  public class ReverendFunUtil {
43  
44      public static String getCurrentDate() {
45          return _instance._getCurrentDate();
46      }
47  
48      public static String getNextDate(String date) {
49          return _instance._getNextDate(date);
50      }
51  
52      public static String getPreviousDate(String date) {
53          return _instance._getPreviousDate(date);
54      }
55  
56      public static boolean hasDate(String date) {
57          return _instance._hasDate(date);
58      }
59  
60      private ReverendFunUtil() {
61          _dates = new ArrayList<String>();
62  
63          String[] dates = StringUtil.split(ContentUtil.get(
64              "com/liferay/portlet/reverendfun/dependencies/dates.txt"), "\n");
65  
66          for (int i = 0; i < dates.length; i++) {
67              _dates.add(dates[i]);
68          }
69      }
70  
71      private String _getCurrentDate() {
72          String firstDates = _dates.get(0);
73  
74          try {
75              Set<String> moreDates = _getMoreDates(firstDates);
76  
77              if (moreDates.size() > 0) {
78                  String firstMoreDates = moreDates.iterator().next();
79  
80                  if (!firstMoreDates.equals(firstDates)) {
81                      _dates.addAll(0, moreDates);
82  
83                      // Trim duplicate dates
84  
85                      Set<String> datesSet = new HashSet<String>();
86  
87                      Iterator<String> itr = _dates.iterator();
88  
89                      while (itr.hasNext()) {
90                          String date = itr.next();
91  
92                          if (datesSet.contains(date)) {
93                              itr.remove();
94                          }
95                          else {
96                              datesSet.add(date);
97                          }
98                      }
99                  }
100             }
101         }
102         catch (Exception e) {
103         }
104 
105         String currentDate = _dates.get(0);
106 
107         return currentDate;
108     }
109 
110     private Set<String> _getMoreDates(String date) {
111         WebCacheItem wci = new ReverendFunWebCacheItem(date);
112 
113         return (Set<String>)WebCachePoolUtil.get(
114             ReverendFunUtil.class.getName() + StringPool.PERIOD + date, wci);
115     }
116 
117     private String _getNextDate(String date) {
118         int pos = Collections.binarySearch(
119             _dates, date, new StringComparator(false, true));
120 
121         if (pos >= 1) {
122             return _dates.get(pos - 1);
123         }
124 
125         return null;
126     }
127 
128     private String _getPreviousDate(String date) {
129         int pos = Collections.binarySearch(
130             _dates, date, new StringComparator(false, true));
131 
132         if (pos > -1 && pos < _dates.size() - 1) {
133             return _dates.get(pos + 1);
134         }
135 
136         return null;
137     }
138 
139     private boolean _hasDate(String date) {
140         int pos = Collections.binarySearch(
141             _dates, date, new StringComparator(false, true));
142 
143         if (pos >= 1) {
144             return true;
145         }
146         else {
147             return false;
148         }
149     }
150 
151     private static ReverendFunUtil _instance = new ReverendFunUtil();
152 
153     private List<String> _dates;
154 
155 }