|
|||||||||||||||||||
| 30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| EraGroup.java | 72.7% | 77.1% | 60% | 72.2% |
|
||||||||||||||
| 1 |
/*
|
|
| 2 |
* Joey and its relative products are published under the terms
|
|
| 3 |
* of the Apache Software License.
|
|
| 4 |
*/
|
|
| 5 |
package org.asyrinx.brownie.core.util;
|
|
| 6 |
|
|
| 7 |
import java.util.Collection;
|
|
| 8 |
import java.util.HashSet;
|
|
| 9 |
import java.util.Iterator;
|
|
| 10 |
import java.util.Locale;
|
|
| 11 |
import java.util.Set;
|
|
| 12 |
|
|
| 13 |
import org.apache.commons.collections.Predicate;
|
|
| 14 |
import org.apache.commons.lang.StringUtils;
|
|
| 15 |
import org.asyrinx.brownie.core.collection.CollectionSqueezer;
|
|
| 16 |
|
|
| 17 |
/**
|
|
| 18 |
* @author akima
|
|
| 19 |
*/
|
|
| 20 |
public class EraGroup { |
|
| 21 |
|
|
| 22 |
/**
|
|
| 23 |
*
|
|
| 24 |
*/
|
|
| 25 | 7 |
public EraGroup() {
|
| 26 | 7 |
super();
|
| 27 |
} |
|
| 28 |
|
|
| 29 |
private final Set eras = new HashSet(); |
|
| 30 |
|
|
| 31 |
/**
|
|
| 32 |
* @param era
|
|
| 33 |
* @return
|
|
| 34 |
*/
|
|
| 35 | 34 |
public boolean add(Era era) { |
| 36 | 34 |
return eras.add(era);
|
| 37 |
} |
|
| 38 |
|
|
| 39 |
/**
|
|
| 40 |
*
|
|
| 41 |
*/
|
|
| 42 | 0 |
public void clear() { |
| 43 | 0 |
eras.clear(); |
| 44 |
} |
|
| 45 |
|
|
| 46 |
/**
|
|
| 47 |
* @param era
|
|
| 48 |
* @return
|
|
| 49 |
*/
|
|
| 50 | 0 |
public boolean contains(Era era) { |
| 51 | 0 |
return eras.contains(era);
|
| 52 |
} |
|
| 53 |
|
|
| 54 |
/**
|
|
| 55 |
* @param c
|
|
| 56 |
* @return
|
|
| 57 |
*/
|
|
| 58 | 0 |
public boolean containsAll(Collection c) { |
| 59 | 0 |
return eras.containsAll(c);
|
| 60 |
} |
|
| 61 |
|
|
| 62 |
/**
|
|
| 63 |
* @return
|
|
| 64 |
*/
|
|
| 65 | 0 |
public boolean isEmpty() { |
| 66 | 0 |
return eras.isEmpty();
|
| 67 |
} |
|
| 68 |
|
|
| 69 |
/**
|
|
| 70 |
* @return
|
|
| 71 |
*/
|
|
| 72 | 0 |
public Iterator iterator() {
|
| 73 | 0 |
return eras.iterator();
|
| 74 |
} |
|
| 75 |
|
|
| 76 |
/**
|
|
| 77 |
* @param era
|
|
| 78 |
* @return
|
|
| 79 |
*/
|
|
| 80 | 0 |
public boolean remove(Era era) { |
| 81 | 0 |
return eras.remove(era);
|
| 82 |
} |
|
| 83 |
|
|
| 84 |
/**
|
|
| 85 |
* @return
|
|
| 86 |
*/
|
|
| 87 | 0 |
public int size() { |
| 88 | 0 |
return eras.size();
|
| 89 |
} |
|
| 90 |
|
|
| 91 |
/**
|
|
| 92 |
* @return @see java.lang.Object#toString()
|
|
| 93 |
*/
|
|
| 94 | 0 |
public String toString() {
|
| 95 | 0 |
return eras.toString();
|
| 96 |
} |
|
| 97 |
|
|
| 98 |
/**
|
|
| 99 |
* 日付がどの年号に当たるのかを返すメソッド。
|
|
| 100 |
*
|
|
| 101 |
* @param d
|
|
| 102 |
* DOCUMENT ME!
|
|
| 103 |
* @return DOCUMENT ME!
|
|
| 104 |
*/
|
|
| 105 | 7 |
public Era getEra(SimpleDate d) {
|
| 106 | 7 |
return getEra(Locale.getDefault(), d);
|
| 107 |
} |
|
| 108 |
|
|
| 109 |
/**
|
|
| 110 |
* 元号を表す文字列からEraを取得する。
|
|
| 111 |
*/
|
|
| 112 | 6 |
public Era findByCaptions(String str) {
|
| 113 | 6 |
if (StringUtils.isEmpty(str))
|
| 114 | 2 |
return null; |
| 115 | 4 |
final Iterator iterator = eras.iterator(); |
| 116 | 4 |
while (iterator.hasNext()) {
|
| 117 | 8 |
final Era era = (Era) iterator.next(); |
| 118 | 8 |
if (era.equals(str))
|
| 119 | 3 |
return era;
|
| 120 |
} |
|
| 121 | 1 |
return null; |
| 122 |
} |
|
| 123 |
|
|
| 124 |
/**
|
|
| 125 |
* 日付がどの年号に当たるのかを返すメソッド。
|
|
| 126 |
*
|
|
| 127 |
* @param locale
|
|
| 128 |
* @param d
|
|
| 129 |
* @return
|
|
| 130 |
*/
|
|
| 131 | 20 |
public Era getEra(Locale locale, SimpleDate d) {
|
| 132 | 20 |
if (d == null) |
| 133 | 0 |
return null; |
| 134 | 20 |
final Iterator iterator = eras.iterator(); |
| 135 | 20 |
final LocaleDateFilter filter = new LocaleDateFilter(locale, d);
|
| 136 | 52 |
while (iterator.hasNext()) {
|
| 137 | 52 |
final Era era = (Era) iterator.next(); |
| 138 | 52 |
if (filter.evaluate(era))
|
| 139 | 20 |
return era;
|
| 140 |
} |
|
| 141 | 0 |
return null; |
| 142 |
} |
|
| 143 |
|
|
| 144 |
/**
|
|
| 145 |
* 指定されたロケールに該当する年号を取得し、Setで返す。
|
|
| 146 |
*
|
|
| 147 |
* @param locale
|
|
| 148 |
* @return
|
|
| 149 |
*/
|
|
| 150 | 5 |
public Set getEras(Locale locale) {
|
| 151 | 5 |
final Set result = new HashSet();
|
| 152 | 5 |
final CollectionSqueezer squeezer = new CollectionSqueezer(
|
| 153 |
new LocaleFilter(locale));
|
|
| 154 | 5 |
squeezer.execute(eras.iterator(), result); |
| 155 | 5 |
return result;
|
| 156 |
} |
|
| 157 |
|
|
| 158 |
/**
|
|
| 159 |
* Date型の引数が該当する年号で何年に当たるのかを返すメソッド。
|
|
| 160 |
*
|
|
| 161 |
* @param d
|
|
| 162 |
* DOCUMENT ME!
|
|
| 163 |
* @return DOCUMENT ME!
|
|
| 164 |
*/
|
|
| 165 | 3 |
public int getEraYear(SimpleDate d) { |
| 166 | 3 |
final Era era = getEra(d); |
| 167 | 3 |
return (era == null) ? d.getYear() : era.getYearAt(d); |
| 168 |
} |
|
| 169 |
|
|
| 170 |
/**
|
|
| 171 |
* 指定されたロケールでDate型の引数が該当する年号で何年に当たるのかを返すメソッド。
|
|
| 172 |
*
|
|
| 173 |
* @param d
|
|
| 174 |
* DOCUMENT ME!
|
|
| 175 |
* @return DOCUMENT ME!
|
|
| 176 |
*/
|
|
| 177 | 4 |
public int getEraYear(Locale locale, SimpleDate d) { |
| 178 | 4 |
final Era era = getEra(locale, d); |
| 179 | 4 |
return (era == null) ? d.getYear() : era.getYearAt(d); |
| 180 |
} |
|
| 181 |
|
|
| 182 |
} |
|
| 183 |
|
|
| 184 |
class LocaleFilter implements Predicate { |
|
| 185 |
|
|
| 186 | 25 |
public LocaleFilter(Locale locale) {
|
| 187 | 25 |
super();
|
| 188 | 25 |
this.locale = locale;
|
| 189 |
} |
|
| 190 |
|
|
| 191 |
final Locale locale; |
|
| 192 |
|
|
| 193 |
/**
|
|
| 194 |
* @param object
|
|
| 195 |
* @return @see org.asyrinx.util.Filter#match(java.lang.Object)
|
|
| 196 |
*/
|
|
| 197 | 74 |
public boolean evaluate(Object object) { |
| 198 | 74 |
if (!(object instanceof Era)) |
| 199 | 0 |
return false; |
| 200 | 74 |
final Era era = (Era) object; |
| 201 | 74 |
return (locale == null) ? true : locale.equals(era.getLocale()); |
| 202 |
} |
|
| 203 |
} |
|
| 204 |
|
|
| 205 |
class LocaleDateFilter extends LocaleFilter { |
|
| 206 |
|
|
| 207 | 20 |
public LocaleDateFilter(Locale locale, SimpleDate date) {
|
| 208 | 20 |
super(locale);
|
| 209 | 20 |
this.date = date;
|
| 210 |
} |
|
| 211 |
|
|
| 212 |
final SimpleDate date; |
|
| 213 |
|
|
| 214 |
/**
|
|
| 215 |
* @param object
|
|
| 216 |
* @return @see org.asyrinx.util.Filter#match(java.lang.Object)
|
|
| 217 |
*/
|
|
| 218 | 52 |
public boolean evaluate(Object object) { |
| 219 | 52 |
if (!super.evaluate(object)) |
| 220 | 9 |
return false; |
| 221 | 43 |
final Era era = (Era) object; |
| 222 | 43 |
return era.getRange().include(date);
|
| 223 |
} |
|
| 224 |
|
|
| 225 |
} |
|
||||||||||