View Javadoc
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.entryfilters;
18  
19  import java.util.Collection;
20  import java.util.zip.ZipEntry;
21  
22  import com.orangesignal.jlha.LhaHeader;
23  
24  /**
25   * 指定されたエントリ名のセットを使ってフィルタを適用するエントリフィルタの実装です。
26   * 
27   * @author Koji Sugisawa
28   */
29  public class EntryNameFilter extends AbstractEntryFilter {
30  
31  	private static final long serialVersionUID = -147779242205375587L;
32  
33  	/**
34  	 * 受け入れるエントリ名のセットを保持します。
35  	 */
36  	private String[] names;
37  
38  	/**
39  	 * 大文字と小文字を区別するかどうかを保持します。
40  	 */
41  	private boolean ignoreCase;
42  
43  	/**
44  	 * 指定された受け入れるエントリ名で大文字と小文字を区別する、このクラスのインスタンスを構築するコンストラクタです。
45  	 * 
46  	 * @param name 受け入れるエントリ名
47  	 */
48  	public EntryNameFilter(final String name) {
49  		this(new String[]{ name }, false);
50  	}
51  
52  	/**
53  	 * 指定された受け入れるエントリ名で、このクラスのインスタンスを構築するコンストラクタです。
54  	 * 
55  	 * @param name 受け入れるエントリ名
56  	 * @param ignoreCase 大文字と小文字を区別するかどうか
57  	 */
58  	public EntryNameFilter(final String name, final boolean ignoreCase) {
59  		this(new String[]{ name }, ignoreCase);
60  	}
61  
62  	/**
63  	 * 指定された受け入れるエントリ名のセットで大文字と小文字を区別する、このクラスのインスタンスを構築するコンストラクタです。
64  	 * 
65  	 * @param names 受け入れるエントリ名のセット
66  	 * @throws IllegalArgumentException {@code names} が {@code null} の場合
67  	 */
68  	public EntryNameFilter(final String[] names) {
69  		this(names, false);
70  	}
71  
72  	/**
73  	 * 指定された受け入れるエントリ名のセットで、このクラスのインスタンスを構築するコンストラクタです。
74  	 * 
75  	 * @param names 受け入れるエントリ名のセット
76  	 * @param ignoreCase 大文字と小文字を区別するかどうか
77  	 * @throws IllegalArgumentException {@code names} が {@code null} の場合
78  	 */
79  	public EntryNameFilter(final String[] names, final boolean ignoreCase) {
80  		if (names == null) {
81  			throw new IllegalArgumentException("Names must not be null");
82  		}
83  		final String[] copy = new String[names.length];
84  		System.arraycopy(names, 0, copy, 0, copy.length);
85  		this.names = copy;
86  		this.ignoreCase = ignoreCase;
87  	}
88  
89  	/**
90  	 * 指定された受け入れるエントリ名のセットで大文字と小文字を区別する、このクラスのインスタンスを構築するコンストラクタです。
91  	 * 
92  	 * @param names 受け入れるエントリ名のセット
93  	 * @throws IllegalArgumentException {@code names} が {@code null} の場合
94  	 */
95  	public EntryNameFilter(final Collection<String> names) {
96  		this(names, false);
97  	}
98  
99  	/**
100 	 * 指定された受け入れるエントリ名のセットで、このクラスのインスタンスを構築するコンストラクタです。
101 	 * 
102 	 * @param names 受け入れるエントリ名のセット
103 	 * @param ignoreCase 大文字と小文字を区別するかどうか
104 	 * @throws IllegalArgumentException {@code names} が {@code null} の場合
105 	 */
106 	public EntryNameFilter(final Collection<String> names, final boolean ignoreCase) {
107 		if (names == null) {
108 			throw new IllegalArgumentException("Names must not be null");
109 		}
110 		this.names = names.toArray(new String[0]);
111 		this.ignoreCase = ignoreCase;
112 	}
113 
114 	/**
115 	 * 指定された ZIP エントリをテストし、エントリが受け入れられる場合は {@code true} そうでない場合は {@code false} を返します。
116 	 * 
117 	 * @param entry テストする ZIP エントリ
118 	 * @return エントリが受け入れられる場合は {@code true}、そうでない場合は {@code false}
119 	 */
120 	@Override
121 	public boolean accept(final ZipEntry entry) {
122 		return accept(entry.getName());
123 	}
124 
125 	/**
126 	 * 指定された LHA エントリをテストし、エントリが受け入れられる場合は {@code true} そうでない場合は {@code false} を返します。
127 	 * 
128 	 * @param entry テストする LHA エントリ
129 	 * @return エントリが受け入れられる場合は {@code true}、そうでない場合は {@code false}
130 	 */
131 	@Override
132 	public boolean accept(final LhaHeader entry) {
133 		return accept(entry.getPath());
134 	}
135 
136 	/**
137 	 * 指定されたエントリ名をテストし、エントリ名が受け入れられる場合は {@code true} そうでない場合は {@code false} を返します。
138 	 * 
139 	 * @param path テストするエントリ名
140 	 * @return エントリが受け入れられる場合は {@code true}、そうでない場合は {@code false}
141 	 */
142 	private boolean accept(final String path) {
143 		for (final String name : names) {
144 			final boolean b = ignoreCase ? path.equalsIgnoreCase(name) : path.equals(name);
145 			if (b) {
146 				return true;
147 			}
148 		}
149 		return false;
150 	}
151 
152 	@Override
153 	public String toString() {
154 		final StringBuilder sb = new StringBuilder();
155 		sb.append(super.toString());
156 		sb.append('(');
157 		if (names != null) {
158 			final int length = names.length;
159 			for (int i = 0; i < length; i++) {
160 				if (i > 0) {
161 					sb.append(',');
162 				}
163 				sb.append(names[i]);
164 			}
165 		}
166 		sb.append(')');
167 		return sb.toString();
168 	}
169 
170 }