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