001/* 002 * Copyright (c) 2009 The openGion Project. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 013 * either express or implied. See the License for the specific language 014 * governing permissions and limitations under the License. 015 */ 016package org.opengion.fukurou.util; 017 018import java.util.List; 019import java.util.ArrayList; 020 021/** 022 * Options.java は、String 型リストをプールするクラスです。 023 * 024 * HTMLのOptionタグのように、複数の文字列をキープしておき、 025 * すべてを連結して出力するような場合に利用できます。 026 * 027 * この実装は同期化されません。 028 * 029 * @version 4.0 030 * @author Kazuhiko Hasegawa 031 * @since JDK5.0, 032 */ 033public final class Options { 034 private final List<String> option = new ArrayList<String>( 100 ); 035 private static final String CRLF = System.getProperty("line.separator"); 036 037 /** 038 * すべての要素をリストから削除します 。 039 * この呼び出しからの復帰後、リストは空になります 。 040 * (例外をスローした場合を除く)。 041 * 042 */ 043 public void clear() { 044 option.clear() ; 045 } 046 047 /** 048 * リストの末尾に指定の文字列を追加します。 049 * value が null の場合は、追加されません。 050 * 051 * @param value リストに追加される文字列 052 */ 053 public void add( final String value ) { 054 if( value != null ) { option.add( value ) ; } 055 } 056 057 /** 058 * リスト内の指定された位置にある要素を返します。 059 * ただし、value が null の場合は、追加されていませんので、 060 * index の順番と 登録データの順番が異なる場合もありますので、 061 * 注意する必要があります。 062 * 063 * @param index 返される要素のインデックス 064 * 065 * @return リスト内の指定された位置にある要素 066 */ 067 public String get( final int index ) { 068 return option.get( index ) ; 069 } 070 071 /** 072 * 登録されているオプションの数を返します。 073 * 074 * @return インタフェース List 内の size 075 * 076 */ 077 public int size() { 078 return option.size() ; 079 } 080 081 /** 082 * リスト内のすべての要素を正しい順序で保持する配列を返します。 083 * 084 * @return リスト内のすべての要素の配列 085 * 086 */ 087 public String[] toArray() { 088 return option.toArray( new String[option.size()] ) ; 089 } 090 091 /** 092 * リストに含まれているデータを オプションタグ形式で返します。 093 * 各オプションタグは整形します。(各リスト毎に改行を入れます。) 094 * 095 * @return Optionタグの形式で返します 096 * 097 */ 098 public String getOption() { 099 return getOption( true ); 100 } 101 102 /** 103 * リストに含まれているデータを オプションタグ形式で返します。 104 * 各オプションタグの整形をする/しないは、パラメータで指定します。 105 * 106 * @param flag 整形する(true)/整形しない(false) 107 * 108 * @return Optionタグの形式で返します 109 */ 110 public String getOption( final boolean flag ) { 111 StringBuilder buf = new StringBuilder( 200 ); 112 113 String crlf = (flag)? CRLF : " "; 114 115 for( int i=0; i<option.size(); i++ ) { 116 buf.append( option.get(i) ).append( crlf ); 117 } 118 return buf.toString(); 119 } 120 121 /** 122 * このオブジェクトの文字列表現を返します。 123 * 基本的にデバッグ目的に使用します。 124 * 125 * @return オブジェクトの文字列表現 126 */ 127 @Override 128 public String toString() { 129 return( getOption( false ) ); 130 } 131}