001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portlet.reverendfun.util;
022:
023: import com.liferay.portal.kernel.util.StringComparator;
024: import com.liferay.portal.kernel.util.StringPool;
025: import com.liferay.portal.kernel.util.StringUtil;
026: import com.liferay.portal.kernel.webcache.WebCacheItem;
027: import com.liferay.portal.kernel.webcache.WebCachePoolUtil;
028: import com.liferay.portal.util.ContentUtil;
029:
030: import java.util.ArrayList;
031: import java.util.Collections;
032: import java.util.HashSet;
033: import java.util.Iterator;
034: import java.util.List;
035: import java.util.Set;
036:
037: /**
038: * <a href="ReverendFunUtil.java.html"><b><i>View Source</i></b></a>
039: *
040: * @author Brian Wing Shun Chan
041: *
042: */
043: public class ReverendFunUtil {
044:
045: public static String getCurrentDate() {
046: return _instance._getCurrentDate();
047: }
048:
049: public static String getNextDate(String date) {
050: return _instance._getNextDate(date);
051: }
052:
053: public static String getPreviousDate(String date) {
054: return _instance._getPreviousDate(date);
055: }
056:
057: public static boolean hasDate(String date) {
058: return _instance._hasDate(date);
059: }
060:
061: private ReverendFunUtil() {
062: _dates = new ArrayList();
063:
064: String[] dates = StringUtil
065: .split(
066: ContentUtil
067: .get("com/liferay/portlet/reverendfun/dependencies/dates.txt"),
068: "\n");
069:
070: for (int i = 0; i < dates.length; i++) {
071: _dates.add(dates[i]);
072: }
073: }
074:
075: private String _getCurrentDate() {
076: String dates0 = (String) _dates.get(0);
077:
078: try {
079: Set moreDates = _getMoreDates(dates0);
080:
081: if (moreDates.size() > 0) {
082: String moreDates0 = (String) moreDates.iterator()
083: .next();
084:
085: if (!moreDates0.equals(dates0)) {
086: _dates.addAll(0, moreDates);
087:
088: // Trim duplicate dates
089:
090: Set datesSet = new HashSet();
091:
092: Iterator itr = _dates.iterator();
093:
094: while (itr.hasNext()) {
095: String date = (String) itr.next();
096:
097: if (datesSet.contains(date)) {
098: itr.remove();
099: } else {
100: datesSet.add(date);
101: }
102: }
103: }
104: }
105: } catch (Exception e) {
106: }
107:
108: String currentDate = (String) _dates.get(1);
109:
110: return currentDate;
111: }
112:
113: private Set _getMoreDates(String date) {
114: WebCacheItem wci = new ReverendFunWebCacheItem(date);
115:
116: return (Set) WebCachePoolUtil.get(ReverendFunUtil.class
117: .getName()
118: + StringPool.PERIOD + date, wci);
119: }
120:
121: private String _getNextDate(String date) {
122: int pos = Collections.binarySearch(_dates, date,
123: new StringComparator(false, true));
124:
125: if (pos > 1) {
126: return (String) _dates.get(pos - 1);
127: }
128:
129: return null;
130: }
131:
132: private String _getPreviousDate(String date) {
133: int pos = Collections.binarySearch(_dates, date,
134: new StringComparator(false, true));
135:
136: if (pos > -1 && pos < _dates.size() - 1) {
137: return (String) _dates.get(pos + 1);
138: }
139:
140: return null;
141: }
142:
143: private boolean _hasDate(String date) {
144: int pos = Collections.binarySearch(_dates, date,
145: new StringComparator(false, true));
146:
147: if (pos >= 1) {
148: return true;
149: } else {
150: return false;
151: }
152: }
153:
154: private static ReverendFunUtil _instance = new ReverendFunUtil();
155:
156: private List _dates;
157:
158: }
|