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