001/**
002 * Copyright 2013 John Ericksen
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 org.androidtransfuse.processor;
017
018import javax.xml.bind.annotation.adapters.XmlAdapter;
019import java.util.Arrays;
020import java.util.HashSet;
021import java.util.Set;
022
023/**
024 * @author John Ericksen
025 */
026public class MergeableTagConverter extends XmlAdapter<String, MergeableTags> {
027
028    private static final String SPLIT = ",";
029
030    @Override
031    public String marshal(MergeableTags mergeableTags) throws Exception {
032        if (mergeableTags == null) {
033            return null;
034        }
035        if (mergeableTags.getTags().isEmpty()) {
036            return null;
037        }
038
039        StringBuilder builder = new StringBuilder();
040
041        boolean first = true;
042
043        for (String s : mergeableTags.getTags()) {
044            if (first) {
045                first = false;
046            } else {
047                builder.append(SPLIT);
048            }
049            builder.append(s);
050        }
051
052        return builder.toString();
053    }
054
055    @Override
056    public MergeableTags unmarshal(String input) throws Exception {
057        Set<String> tagValues = new HashSet<String>();
058
059        if(input != null && !input.isEmpty()){
060            String[] splitInput = input.split(SPLIT);
061            tagValues.addAll(Arrays.asList(splitInput));
062        }
063
064        return new MergeableTags(tagValues);
065    }
066}