1 /*
2 * Copyright 2013 the original author or authors.
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, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package com.orangesignal.csv.bean;
18
19 import java.util.List;
20
21 import com.orangesignal.csv.filters.CsvNamedValueFilter;
22
23 /**
24 * Java プログラム要素と区切り文字形式データアクセスの操作を簡素化するヘルパークラスを提供します。
25 *
26 * @author Koji Sugisawa
27 * @since 1.4.0
28 */
29 public class CsvBeanTemplate<T> extends AbstractCsvBeanTemplate<T, CsvBeanTemplate<T>> implements CsvBeanOperation<CsvBeanTemplate<T>> {
30
31 /**
32 * Java プログラム要素へデータを設定する名前群を保持します。
33 */
34 private String[] includes;
35
36 /**
37 * Java プログラム要素へデータを設定しない名前群を保持します。
38 */
39 private String[] excludes;
40
41 /**
42 * 区切り文字形式データフィルタを保持します。
43 */
44 private CsvNamedValueFilter filter;
45
46 // ------------------------------------------------------------------------
47 // 利便性のための静的メソッド
48
49 /**
50 * 新しい {@link CsvBeanTemplate} のインスタンスを返します。
51 *
52 * @param type Java プログラム要素の型
53 * @return 新しい {@link CsvBeanTemplate} のインスタンス
54 * @throws IllegalArgumentException {@code type} が {@code null} の場合。
55 */
56 public static <T> CsvBeanTemplate<T> newInstance(final Class<T> type) {
57 return new CsvBeanTemplate<T>(type);
58 }
59
60 // -----------------------------------------------------------------------
61 // コンストラクタ
62
63 /**
64 * コンストラクタです。
65 *
66 * @param type Java プログラム要素の型
67 * @throws IllegalArgumentException {@code type} が {@code null} の場合。
68 */
69 public CsvBeanTemplate(final Class<T> type) {
70 super(type);
71 }
72
73 // -----------------------------------------------------------------------
74 // オーバーライド メソッド
75
76 @Override
77 public CsvBeanTemplate<T> includes(final String... names) {
78 if (excludes != null && excludes.length > 0) {
79 throw new IllegalArgumentException("Only includes or excludes may be specified.");
80 }
81 this.includes = names;
82 return this;
83 }
84
85 @Override
86 public CsvBeanTemplate<T> excludes(final String... names) {
87 if (includes != null && includes.length > 0) {
88 throw new IllegalArgumentException("Only includes or excludes may be specified.");
89 }
90 this.excludes = names;
91 return this;
92 }
93
94 @Override
95 public CsvBeanTemplate<T> filter(final CsvNamedValueFilter filter) {
96 this.filter = filter;
97 return this;
98 }
99
100 // -----------------------------------------------------------------------
101 // パブリック メソッド
102
103 /**
104 * 指定された区切り文字形式データの値リストが含まれる必要があるかどうかを判定します。
105 *
106 * @param columnNames 区切り文字形式データの項目名リスト
107 * @param values 区切り文字形式データの項目値のリスト
108 * @return {@code values} が含まれる必要がある場合は {@code true}
109 */
110 public boolean isAccept(final List<String> columnNames, final List<String> values) {
111 return filter != null && !filter.accept(columnNames, values);
112 }
113
114 /**
115 * 指定された名前が Java プログラム要素としてデータを設定すべき名前かどうかを返します。
116 *
117 * @param name 名前
118 * @return Java プログラム要素としてデータを設定すべき名前の場合は {@code true} それ以外の場合は {@code false}
119 */
120 public boolean isTargetName(final String name) {
121 if (excludes != null && excludes.length > 0) {
122 for (final String propertyName : excludes) {
123 if (propertyName.equals(name)) {
124 return false;
125 }
126 }
127 return true;
128 }
129 if (includes != null && includes.length > 0) {
130 for (final String propertyName : includes) {
131 if (propertyName.equals(name)) {
132 return true;
133 }
134 }
135 return false;
136 }
137 return true;
138 }
139
140 }