001/* 
002 * Copyright 2018 Leibniz-Institut für Analytische Wissenschaften – ISAS – e.V..
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, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package uk.ac.ebi.pride.jmztab2.model;
017
018/**
019 * In mzTab, using 0-false, 1-true to express the boolean value.
020 *
021 * @author qingwei
022 * @since 06/02/13
023 * 
024 */
025public enum MZBoolean {
026    True("1"), False("0");
027    private String value;
028
029    /**
030     * "0" for false, "1" for true.
031     * @param value
032     */
033    MZBoolean(String value) {
034        this.value = value;
035    }
036
037    /** {@inheritDoc} */
038    @Override
039    public String toString() {
040        return value;
041    }
042    
043    /**
044     * <p>Convert to a native Boolean.</p>
045     *
046     * @return a {@link java.lang.Boolean} object.
047     */
048    public Boolean toBoolean() {
049        if(this == MZBoolean.True) {
050            return Boolean.TRUE;
051        }
052        return Boolean.FALSE;
053    }
054
055    /**
056     * <p>Find the MZBoolean for the given string representation.</p>
057     *
058     * @param booleanLabel "0" or "1" which used to define a boolean used in mzTab.
059     * @return null if can not recognize the boolean label.
060     */
061    public static MZBoolean findBoolean(String booleanLabel) {
062        booleanLabel = booleanLabel.trim();
063        try {
064            Integer id = new Integer(booleanLabel);
065            MZBoolean mzBoolean = null;
066            switch (id) {
067                case 0:
068                    mzBoolean = MZBoolean.False;
069                    break;
070                case 1:
071                    mzBoolean = MZBoolean.True;
072                    break;
073            }
074            return mzBoolean;
075        } catch (NumberFormatException e) {
076            return null;
077        }
078    }
079}