Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
CubbyFunctions |
|
| 0.0;0 |
1 | /* | |
2 | * Copyright 2004-2008 the Seasar Foundation and the Others. | |
3 | * | |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); | |
5 | * you may not use this file except in compliance with the License. | |
6 | * You may obtain a copy of the License at | |
7 | * | |
8 | * http://www.apache.org/licenses/LICENSE-2.0 | |
9 | * | |
10 | * Unless required by applicable law or agreed to in writing, software | |
11 | * distributed under the License is distributed on an "AS IS" BASIS, | |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, | |
13 | * either express or implied. See the License for the specific language | |
14 | * governing permissions and limitations under the License. | |
15 | */ | |
16 | package org.seasar.cubby.util; | |
17 | ||
18 | import java.text.SimpleDateFormat; | |
19 | import java.util.Arrays; | |
20 | import java.util.Collection; | |
21 | import java.util.Date; | |
22 | import java.util.Map; | |
23 | ||
24 | /** | |
25 | * CubbyのJSP EL functionsを提供します。 | |
26 | * | |
27 | * @author baba | |
28 | * @since 1.0.0 | |
29 | */ | |
30 | 0 | public class CubbyFunctions { |
31 | ||
32 | /** | |
33 | * 配列やコレクションに指定したオブジェクトが含まれるかどうかを判定します。 | |
34 | * | |
35 | * @param collection | |
36 | * 配列や{@link Collection コレクション} | |
37 | * @param obj | |
38 | * 配列やコレクションにあるかどうかを調べる要素 | |
39 | * @return 配列やコレクションに指定したオブジェクトが含まれる場合は <code>true</code>、そうでない場合は | |
40 | * <code>false</code> | |
41 | */ | |
42 | public static Boolean contains(final Object collection, final Object obj) { | |
43 | 2 | if (collection instanceof Collection) { |
44 | 2 | return _contains((Collection<?>) collection, obj); |
45 | 0 | } else if (collection != null && collection.getClass().isArray()) { |
46 | 0 | return _contains(Arrays.asList((Object[]) collection), obj); |
47 | } else { | |
48 | 0 | return false; |
49 | } | |
50 | } | |
51 | ||
52 | /** | |
53 | * 指定された要素が{@link Collection}内にあるかどうかを示します。 | |
54 | * | |
55 | * @param collection | |
56 | * コレクション | |
57 | * @param obj | |
58 | * コレクションにあるかどうかを調べる要素 | |
59 | * @return 指定された要素が{@link Collection}内にある場合は <code>true</code>、そうでない場合は | |
60 | * <code>false</code> | |
61 | */ | |
62 | private static Boolean _contains(final Collection<?> collection, | |
63 | final Object obj) { | |
64 | 2 | return collection.contains(obj); |
65 | } | |
66 | ||
67 | /** | |
68 | * {@link Map}に指定したキーが含まれるかどうかを判定します。 | |
69 | * | |
70 | * @param map | |
71 | * マップ | |
72 | * @param key | |
73 | * マップにあるかどうかが判定されるキー | |
74 | * @return {@link Map}に指定したキーが含まれる場合は <code>true</code>、そうでない場合は | |
75 | * <code>false</code> | |
76 | */ | |
77 | public static Boolean containsKey(final Map<?, ?> map, final Object key) { | |
78 | 0 | return map.containsKey(key); |
79 | } | |
80 | ||
81 | /** | |
82 | * {@link Map}に指定した値が含まれるかどうかを判定します。 | |
83 | * | |
84 | * @param map | |
85 | * マップ | |
86 | * @param value | |
87 | * マップにあるかどうかを判定される値 | |
88 | * @return {@link Map}に指定した値が含まれる場合は <code>true</code>、そうでない場合は | |
89 | * <code>false</code> | |
90 | */ | |
91 | public static Boolean containsValue(final Map<?, ?> map, final Object value) { | |
92 | 0 | return map.containsValue(value); |
93 | } | |
94 | ||
95 | /** | |
96 | * 指定したカンマ区切りの文字列をインデックス値でサイクルして出力します。 | |
97 | * <p> | |
98 | * 主に行毎に色分けする場合に CSS のクラス名を出力する場合に使用します。 | |
99 | * </p> | |
100 | * | |
101 | * @param index | |
102 | * インデックス | |
103 | * @param classNames | |
104 | * カンマ区切りの文字列 | |
105 | * @return 指定したインデックスに対応する文字列 | |
106 | */ | |
107 | public static String odd(final Integer index, final String classNames) { | |
108 | 0 | final String[] c = classNames.split(","); |
109 | 0 | return c[index % c.length]; |
110 | } | |
111 | ||
112 | /** | |
113 | * HTMLをエスケープします。 | |
114 | * <p> | |
115 | * JSTLのoutタグの代わりに使用します。EL式で出力された文字列はエスケープされないため、エスケープを行いたい場合はこのfunctionを使用します。 | |
116 | * </p> | |
117 | * | |
118 | * @param str | |
119 | * エスケープする文字列 | |
120 | * @return エスケープされた HTML | |
121 | */ | |
122 | public static String out(final Object str) { | |
123 | 102 | return str == null ? "" : CubbyUtils.escapeHtml(str.toString()); |
124 | } | |
125 | ||
126 | /** | |
127 | * {@link Date}型のオブジェクトをフォーマットして出力します。 | |
128 | * <p> | |
129 | * JSTL の dateFormat タグの代わりに使用します。 | |
130 | * </p> | |
131 | * | |
132 | * @param date | |
133 | * 日付/時刻文字列にフォーマットする日付/時刻値 | |
134 | * @param pattern | |
135 | * 日付と時刻のフォーマットを記述するパターン | |
136 | * @return フォーマットされた日付/時刻文字列 | |
137 | */ | |
138 | public static String dateFormat(final Object date, final String pattern) { | |
139 | 0 | if (date instanceof Date) { |
140 | 0 | final SimpleDateFormat format = new SimpleDateFormat(pattern); |
141 | 0 | return format.format(date); |
142 | } else { | |
143 | 0 | return ""; |
144 | } | |
145 | } | |
146 | ||
147 | } |