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.utils.parser;
017
018import java.util.Collection;
019import java.util.Set;
020import java.util.SortedMap;
021import java.util.TreeMap;
022import uk.ac.ebi.pride.jmztab2.model.IMZTabColumn;
023import uk.ac.ebi.pride.jmztab2.model.MZTabColumnFactory;
024
025/**
026 * Create and maintain a couple of mappings between physical position and logical position.
027 * Physical position: Integer, the position of mzTab file.
028 * Logical position: String, the internal order of specification.
029 *
030 * @author qingwei
031 * @since 16/10/13
032 * 
033 */
034public final class PositionMapping {
035    // physicalPosition <--> logicalPosition
036    private final SortedMap<Integer, String> mappings = new TreeMap<>();
037
038    /**
039     * <p>Constructor for PositionMapping.</p>
040     *
041     * @param factory a {@link uk.ac.ebi.pride.jmztab2.model.MZTabColumnFactory} object.
042     * @param headerLine a {@link java.lang.String} object.
043     */
044    public PositionMapping(MZTabColumnFactory factory, String headerLine) {
045        this(factory, headerLine.split("\t"));
046    }
047
048    /**
049     * <p>Constructor for PositionMapping.</p>
050     *
051     * @param factory a {@link uk.ac.ebi.pride.jmztab2.model.MZTabColumnFactory} object.
052     * @param headerList an array of {@link java.lang.String} objects.
053     */
054    public PositionMapping(MZTabColumnFactory factory, String[] headerList) {
055        String header;
056        for (int physicalPosition = 0; physicalPosition < headerList.length; physicalPosition++) {
057            header = headerList[physicalPosition];
058            IMZTabColumn column = factory.findColumnByHeader(header);
059            if (column != null) {
060                put(physicalPosition, column.getLogicPosition());
061            }
062        }
063    }
064
065    /**
066     * <p>put.</p>
067     *
068     * @param physicalPosition a {@link java.lang.Integer} object.
069     * @param logicalPosition a {@link java.lang.String} object.
070     */
071    public void put(Integer physicalPosition, String logicalPosition) {
072        this.mappings.put(physicalPosition, logicalPosition);
073    }
074
075    /**
076     * <p>isEmpty.</p>
077     *
078     * @return a boolean.
079     */
080    public boolean isEmpty() {
081        return mappings.isEmpty();
082    }
083
084    /**
085     * <p>size.</p>
086     *
087     * @return a int.
088     */
089    public int size() {
090        return mappings.size();
091    }
092
093    /**
094     * <p>containsKey.</p>
095     *
096     * @param key a {@link java.lang.Integer} object.
097     * @return a boolean.
098     */
099    public boolean containsKey(Integer key) {
100        return mappings.containsKey(key);
101    }
102
103    /**
104     * <p>keySet.</p>
105     *
106     * @return a {@link java.util.Set} object.
107     */
108    public Set<Integer> keySet() {
109        return mappings.keySet();
110    }
111
112    /**
113     * <p>values.</p>
114     *
115     * @return a {@link java.util.Collection} object.
116     */
117    public Collection<String> values() {
118        return mappings.values();
119    }
120
121    /**
122     * <p>get.</p>
123     *
124     * @param key a {@link java.lang.Integer} object.
125     * @return a {@link java.lang.String} object.
126     */
127    public String get(Integer key) {
128        return mappings.get(key);
129    }
130
131    /**
132     * Exchange key and value to "LogicalPosition, PhysicalPosition". This method used to simply the locate
133     * operation by logical position to physical position.
134     *
135     * @return a {@link java.util.SortedMap} object.
136     */
137    public SortedMap<String, Integer> reverse() {
138        SortedMap<String, Integer> reverseMappings = new TreeMap<>();
139
140        String logicalPosition;
141        for (Integer physicalPosition : mappings.keySet()) {
142            logicalPosition = mappings.get(physicalPosition);
143            reverseMappings.put(logicalPosition, physicalPosition);
144        }
145
146        return reverseMappings;
147    }
148}