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 */
016 package org.opengion.fukurou.util;
017
018 import java.util.List;
019 import 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 */
033 public 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 }