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.process;
017
018import java.util.List;
019import java.util.ArrayList;
020
021/**
022 * LineModelFilter は、フィルター条件をチェックして、LineModel のフィルタリング
023 * を判定する実装クラスです。
024 * フィルター条件 には、パッケージプライベートな、FilterOperation enum を
025 * 指定します。
026 *
027 * 注意:このクラスは、同期処理されていません。
028 *
029 * @version  4.0
030 * @author   Kazuhiko Hasegawa
031 * @since    JDK5.0,
032 */
033public class LineModelFilter {
034        private final List<FilterOperation> opes = new ArrayList<FilterOperation>();
035        private final List<String> clms = new ArrayList<String>();
036        private final List<String> vals = new ArrayList<String>();
037        private int[] clmNo = null;     // 最初の LineModel で構築します。
038        private int   size  = 0;
039
040        /**
041         * フィルター条件を指定します。
042         * オペレータには、FilterOperation enum を 使用してください。
043         * 指定できません。
044         *
045         * @param   ope フィルター条件のオペレーション(PREFIX, SUFFIX, INSTR, EQUALS, MATCH, UNMATCH)
046         * @param   clm 条件判定するカラム名
047         * @param   val 条件値
048         */
049        public void add( final FilterOperation ope,final String clm,final String val ) {
050        //      if( OPERATOR.indexOf( ope ) < 0 ) {
051        //              String errMsg = "オペレータには、prefix,suffix,instr,equals,match,unmatch 以外は指定できません。" ;
052        //              throw new RuntimeException( errMsg );
053        //      }
054
055                opes.add( ope );
056                clms.add( clm );
057                vals.add( val );
058        }
059
060        /**
061         * LineModelを指定して、条件にマッチするか、チェックします。
062         *
063         * @param       data    処理対象のLineModel
064         *
065         * @return  演算結果がすべて成立する場合:true/不成立:false
066         */
067        public boolean filter( final LineModel data ) {
068                if( clmNo == null ) {
069                        size  = clms.size();
070                        clmNo = new int[size];
071                        for( int i=0; i<size; i++ ) {
072                                clmNo[i] = data.getColumnNo( clms.get(i) );
073                        }
074                }
075
076                boolean exist = true;
077                for( int i=0; i<size; i++ ) {
078                        Object value = data.getValue(clmNo[i]);
079                        if( value == null ) { exist = false; break; }
080
081                        FilterOperation ope = opes.get(i);
082                        String clmData = String.valueOf( value );
083                        String argment = vals.get(i);
084
085                        final boolean flag ;
086                        switch( ope ) {
087                                case PREFIX:    flag = clmData.startsWith( argment );   break;
088                                case SUFFIX:    flag = clmData.endsWith( argment );             break;
089                                case INSTR:             flag = clmData.contains( argment );             break;
090                                case EQUALS:    flag = clmData.equalsIgnoreCase( argment ); break;
091                                case MATCH:             flag = clmData.matches( argment );              break;
092                                case UNMATCH:   flag = ! clmData.matches( argment );    break;
093                                default :               flag = false;                                                   break;
094                        }
095
096                        if( !flag ) { exist = false; break; }
097                }
098                return exist;
099        }
100
101        /**
102         * このオブジェクトの内部文字列表現を返します。
103         *
104         * オペレーション(カラム,値) の羅列 です。
105         *
106         * @return 内部文字列表現
107         */
108        @Override
109        public String toString() {
110                int size = opes.size();
111                StringBuilder rtn = new StringBuilder();
112                for( int i=0; i<size; i++ ) {
113                        rtn.append( opes.get(i) ).append( '(' );
114                        rtn.append( clms.get(i) ).append( ',' );
115                        rtn.append( vals.get(i) ).append( ") + " );
116                }
117                return rtn.toString();
118        }
119}