001/*
002 *                    BioJava development code
003 *
004 * This code may be freely distributed and modified under the
005 * terms of the GNU Lesser General Public Licence.  This should
006 * be distributed with the code.  If you do not have a copy,
007 * see:
008 *
009 *      http://www.gnu.org/copyleft/lesser.html
010 *
011 * Copyright for this code is held jointly by the individual
012 * authors.  These should be listed in @author doc comments.
013 *
014 * For more information on the BioJava project and its aims,
015 * or to join the biojava-l mailing list, visit the home page
016 * at:
017 *
018 *      http://www.biojava.org/
019 *
020 * Created on 2011-11-20
021 *
022 */
023
024package org.biojava.nbio.ws.alignment.qblast;
025
026import java.util.Map;
027
028
029
030/**
031 * Transforms Map to String. Used by {@linkplain NCBIQBlastService} to join
032 * given map of arguments to a single String to pass to QBlast service
033 *
034 * @author Gediminas Rimsa
035 */
036public class MapToStringTransformer {
037        private String mappingSequence;
038        private String separatorSequence;
039        private String nullValue;
040
041        /**
042         * Creates {@code MapToStringTransformer} with defaults:
043         *
044         * <pre>
045         * mappingSequence = "=";
046         * separatorSequence = "&";
047         * nullValue = "null";
048         * </pre>
049         */
050        public MapToStringTransformer() {
051                this("=", "&", "null");
052        }
053
054        /**
055         * Creates {@code MapToStringTransformer} with given values
056         *
057         * @param mappingSequence sequence inserted between {@code key} and
058         *            {@code value}
059         * @param separatorSequence sequence inserted between every pair of
060         *            {@code Map} entries
061         * @param nullValue sequence inserted for every {@code null} key or value
062         */
063        public MapToStringTransformer(String mappingSequence, String separatorSequence, String nullValue) {
064                this.setMappingSequence(mappingSequence);
065                this.setSeparatorSequence(separatorSequence);
066                this.setNullValue(nullValue);
067        }
068
069        /**
070         * Transforms {@code Map} to {@code String}, representing every entry as
071         * {@code key} {@code mappingSequence} {@code value} , joined by
072         * {@code separatorSequence}
073         * <p>
074         * Calls {@code toString()} for keys and values, replacing {@code null} with
075         * the value of {@code nullValue} property
076         * <p>
077         * For example, if we have a map with two entries: {@code ("key1", "1")} and
078         * {@code ("key2", "2")} this method would return {@code "key1=1&key2=2"} if
079         * {@code mappingSequence} is "=" and separator sequence is "&";
080         *
081         * @param map map of arguments
082         * @return String resulting string
083         */
084        public String transform(Map<?, ?> map) {
085                StringBuilder sb = new StringBuilder();
086                for (Object key : map.keySet()) {
087                        sb.append(getSeparatorSequence());
088                        String keyString = key != null ? key.toString() : getNullValue();
089                        sb.append(keyString);
090                        sb.append(getMappingSequence());
091                        String valueString = map.get(key) != null ? map.get(key).toString() : getNullValue();
092                        sb.append(valueString);
093                }
094                return sb.substring(1);
095        }
096
097        public String getMappingSequence() {
098                return mappingSequence;
099        }
100
101        public void setMappingSequence(String mappingSequence) {
102                this.mappingSequence = mappingSequence;
103        }
104
105        public String getSeparatorSequence() {
106                return separatorSequence;
107        }
108
109        public void setSeparatorSequence(String separatorSequence) {
110                this.separatorSequence = separatorSequence;
111        }
112
113        public String getNullValue() {
114                return nullValue;
115        }
116
117        public void setNullValue(String nullValue) {
118                this.nullValue = nullValue;
119        }
120}