1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.reverendfun.util;
16  
17  import com.liferay.portal.kernel.util.StringComparator;
18  import com.liferay.portal.kernel.util.StringPool;
19  import com.liferay.portal.kernel.util.StringUtil;
20  import com.liferay.portal.kernel.webcache.WebCacheItem;
21  import com.liferay.portal.kernel.webcache.WebCachePoolUtil;
22  import com.liferay.portal.util.ContentUtil;
23  
24  import java.util.ArrayList;
25  import java.util.Collections;
26  import java.util.HashSet;
27  import java.util.Iterator;
28  import java.util.List;
29  import java.util.Set;
30  
31  /**
32   * <a href="ReverendFunUtil.java.html"><b><i>View Source</i></b></a>
33   *
34   * @author Brian Wing Shun Chan
35   */
36  public class ReverendFunUtil {
37  
38      public static String getCurrentDate() {
39          return _instance._getCurrentDate();
40      }
41  
42      public static String getNextDate(String date) {
43          return _instance._getNextDate(date);
44      }
45  
46      public static String getPreviousDate(String date) {
47          return _instance._getPreviousDate(date);
48      }
49  
50      public static boolean hasDate(String date) {
51          return _instance._hasDate(date);
52      }
53  
54      private ReverendFunUtil() {
55          _dates = new ArrayList<String>();
56  
57          String[] dates = StringUtil.split(ContentUtil.get(
58              "com/liferay/portlet/reverendfun/dependencies/dates.txt"), "\n");
59  
60          for (int i = 0; i < dates.length; i++) {
61              _dates.add(dates[i]);
62          }
63      }
64  
65      private String _getCurrentDate() {
66          String firstDates = _dates.get(0);
67  
68          try {
69              Set<String> moreDates = _getMoreDates(firstDates);
70  
71              if (moreDates.size() > 0) {
72                  String firstMoreDates = moreDates.iterator().next();
73  
74                  if (!firstMoreDates.equals(firstDates)) {
75                      _dates.addAll(0, moreDates);
76  
77                      // Trim duplicate dates
78  
79                      Set<String> datesSet = new HashSet<String>();
80  
81                      Iterator<String> itr = _dates.iterator();
82  
83                      while (itr.hasNext()) {
84                          String date = itr.next();
85  
86                          if (datesSet.contains(date)) {
87                              itr.remove();
88                          }
89                          else {
90                              datesSet.add(date);
91                          }
92                      }
93                  }
94              }
95          }
96          catch (Exception e) {
97          }
98  
99          String currentDate = _dates.get(0);
100 
101         return currentDate;
102     }
103 
104     private Set<String> _getMoreDates(String date) {
105         WebCacheItem wci = new ReverendFunWebCacheItem(date);
106 
107         return (Set<String>)WebCachePoolUtil.get(
108             ReverendFunUtil.class.getName() + StringPool.PERIOD + date, wci);
109     }
110 
111     private String _getNextDate(String date) {
112         int pos = Collections.binarySearch(
113             _dates, date, new StringComparator(false, true));
114 
115         if (pos >= 1) {
116             return _dates.get(pos - 1);
117         }
118 
119         return null;
120     }
121 
122     private String _getPreviousDate(String date) {
123         int pos = Collections.binarySearch(
124             _dates, date, new StringComparator(false, true));
125 
126         if (pos > -1 && pos < _dates.size() - 1) {
127             return _dates.get(pos + 1);
128         }
129 
130         return null;
131     }
132 
133     private boolean _hasDate(String date) {
134         int pos = Collections.binarySearch(
135             _dates, date, new StringComparator(false, true));
136 
137         if (pos >= 1) {
138             return true;
139         }
140         else {
141             return false;
142         }
143     }
144 
145     private static ReverendFunUtil _instance = new ReverendFunUtil();
146 
147     private List<String> _dates;
148 
149 }