1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  public class ReverendFunUtil {
45  
46      public static String getCurrentDate() {
47          return _instance._getCurrentDate();
48      }
49  
50      public static String getNextDate(String date) {
51          return _instance._getNextDate(date);
52      }
53  
54      public static String getPreviousDate(String date) {
55          return _instance._getPreviousDate(date);
56      }
57  
58      public static boolean hasDate(String date) {
59          return _instance._hasDate(date);
60      }
61  
62      private ReverendFunUtil() {
63          _dates = new ArrayList<String>();
64  
65          String[] dates = StringUtil.split(ContentUtil.get(
66              "com/liferay/portlet/reverendfun/dependencies/dates.txt"), "\n");
67  
68          for (int i = 0; i < dates.length; i++) {
69              _dates.add(dates[i]);
70          }
71      }
72  
73      private String _getCurrentDate() {
74          String firstDates = _dates.get(0);
75  
76          try {
77              Set<String> moreDates = _getMoreDates(firstDates);
78  
79              if (moreDates.size() > 0) {
80                  String firstMoreDates = moreDates.iterator().next();
81  
82                  if (!firstMoreDates.equals(firstDates)) {
83                      _dates.addAll(0, moreDates);
84  
85                      // Trim duplicate dates
86  
87                      Set<String> datesSet = new HashSet<String>();
88  
89                      Iterator<String> itr = _dates.iterator();
90  
91                      while (itr.hasNext()) {
92                          String date = itr.next();
93  
94                          if (datesSet.contains(date)) {
95                              itr.remove();
96                          }
97                          else {
98                              datesSet.add(date);
99                          }
100                     }
101                 }
102             }
103         }
104         catch (Exception e) {
105         }
106 
107         String currentDate = _dates.get(0);
108 
109         return currentDate;
110     }
111 
112     private Set<String> _getMoreDates(String date) {
113         WebCacheItem wci = new ReverendFunWebCacheItem(date);
114 
115         return (Set<String>)WebCachePoolUtil.get(
116             ReverendFunUtil.class.getName() + StringPool.PERIOD + date, wci);
117     }
118 
119     private String _getNextDate(String date) {
120         int pos = Collections.binarySearch(
121             _dates, date, new StringComparator(false, true));
122 
123         if (pos >= 1) {
124             return _dates.get(pos - 1);
125         }
126 
127         return null;
128     }
129 
130     private String _getPreviousDate(String date) {
131         int pos = Collections.binarySearch(
132             _dates, date, new StringComparator(false, true));
133 
134         if (pos > -1 && pos < _dates.size() - 1) {
135             return _dates.get(pos + 1);
136         }
137 
138         return null;
139     }
140 
141     private boolean _hasDate(String date) {
142         int pos = Collections.binarySearch(
143             _dates, date, new StringComparator(false, true));
144 
145         if (pos >= 1) {
146             return true;
147         }
148         else {
149             return false;
150         }
151     }
152 
153     private static ReverendFunUtil _instance = new ReverendFunUtil();
154 
155     private List<String> _dates;
156 
157 }