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    }