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.model;
017
018import java.util.regex.Pattern ;
019
020/**
021 * Native Type(int型やlong型等)を管理するEnum型です。
022 *
023 * @og.group 画面表示
024 *
025 * @version  4.0
026 * @author   Hiroki Nakamura
027 * @since    JDK5.0,
028 */
029public enum NativeType {
030
031        /** NATIVEの型 [int] の識別コード (INT)  */
032        INT             ,
033
034        /** NATIVEの型 [long] の識別コード (LONG)  */
035        LONG    ,
036
037        /** NATIVEの型 [double] の識別コード (DOUBLE)  */
038        DOUBLE  ,
039
040        /** NATIVEの型 [String] の識別コード (STRING)  */
041        STRING  ,
042
043        /** NATIVEの型 [Calendar] の識別コード (CALENDAR)  */
044        CALENDAR ;
045
046        private static final Pattern P_INT              = Pattern.compile("-?[0-9]{1,9}");
047        private static final Pattern P_LONG             = Pattern.compile("-?[0-9]+");
048        private static final Pattern P_DOUBLE   = Pattern.compile("-?[0-9]+\\.?[0-9]+");
049        private static final Pattern P_CALENDAR = Pattern.compile("[0-9]{4}/[01]{1}[0-9]{1}/[0-3]{1}[0-9]{1}");
050
051        /**
052         * 指定の文字列が、どの、NativeType なのか、判定します。
053         *
054         * 判定ロジックは、以下の様にしています。
055         * <pre>
056         *   INT      = Pattern.compile("-?[0-9]{1,9}");
057         *   LONG     = Pattern.compile("-?[0-9]+");
058         *   DOUBLE   = Pattern.compile("-?[0-9]+\\.?[0-9]+");
059         *   CALENDAR = Pattern.compile("[0-9]{4}/[01]{1}[0-9]{1}/[0-3]{1}[0-9]{1}");
060         * </pre>
061         *
062         * @og.rev 5.1.8.0 (2010/07/01) 新規追加
063         *
064         * @param       str     判定する文字列
065         *
066         * @return      判定結果
067         */
068        public static NativeType getType( final String str ) {
069                if( str == null ) { return STRING   ; }
070                else if( P_INT.matcher(      str ).matches() ) { return INT      ; }
071                else if( P_LONG.matcher(     str ).matches() ) { return LONG     ; }
072                else if( P_DOUBLE.matcher(   str ).matches() ) { return DOUBLE   ; }
073                else if( P_CALENDAR.matcher( str ).matches() ) { return CALENDAR ; }
074                else                                           { return STRING   ; }
075        }
076}