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.model.manifest;
017
018import org.androidtransfuse.annotations.*;
019
020import javax.xml.bind.annotation.adapters.XmlAdapter;
021import java.util.HashMap;
022import java.util.Map;
023
024/**
025 * @author John Ericksen
026 */
027public class LabeledConverter<T extends Labeled> extends XmlAdapter<String, T> {
028
029    public static final class InstallLocationConverter extends LabeledConverter<InstallLocation>{
030        public InstallLocationConverter() {
031            super(InstallLocation.class, InstallLocation.values());
032        }
033    }
034
035    public static final class LaunchModeConverter extends LabeledConverter<LaunchMode>{
036        public LaunchModeConverter() {
037            super(LaunchMode.class, LaunchMode.values());
038        }
039    }
040
041    public static final class ProtectionLevelConverter extends LabeledConverter<ProtectionLevel>{
042        public ProtectionLevelConverter() {
043            super(ProtectionLevel.class, ProtectionLevel.values());
044        }
045    }
046
047    public static final class ReqKeyboardTypeConverter extends LabeledConverter<ReqKeyboardType>{
048        public ReqKeyboardTypeConverter() {
049            super(ReqKeyboardType.class, ReqKeyboardType.values());
050        }
051    }
052
053    public static final class ReqNavigationConverter extends LabeledConverter<ReqNavigation>{
054        public ReqNavigationConverter() {
055            super(ReqNavigation.class, ReqNavigation.values());
056        }
057    }
058
059    public static final class ReqTouchScreenConverter extends LabeledConverter<ReqTouchScreen>{
060        public ReqTouchScreenConverter() {
061            super(ReqTouchScreen.class, ReqTouchScreen.values());
062        }
063    }
064
065    public static final class ScreenDensityConverter extends LabeledConverter<ScreenDensity>{
066        public ScreenDensityConverter() {
067            super(ScreenDensity.class, ScreenDensity.values());
068        }
069    }
070
071    public static final class ScreenOrientationConverter extends LabeledConverter<ScreenOrientation>{
072        public ScreenOrientationConverter() {
073            super(ScreenOrientation.class, ScreenOrientation.values());
074        }
075    }
076
077    public static final class ScreenSizeConverter extends LabeledConverter<ScreenSize>{
078        public ScreenSizeConverter() {
079            super(ScreenSize.class, ScreenSize.values());
080        }
081    }
082
083    public static final class UIOptionsConverter extends LabeledConverter<UIOptions>{
084        public UIOptionsConverter() {
085            super(UIOptions.class, UIOptions.values());
086        }
087    }
088
089    public static final class WindowSoftInputModeConverter extends LabeledConverter<WindowSoftInputMode>{
090        public WindowSoftInputModeConverter() {
091            super(WindowSoftInputMode.class, WindowSoftInputMode.values());
092        }
093    }
094
095    private final Class<T> labeled;
096    private final Map<String, T> labelMap;
097
098    public LabeledConverter(Class<T> labeled, T[] values) {
099        this.labeled = labeled;
100        labelMap = new HashMap<String, T>();
101        for (T value : values) {
102            labelMap.put(value.getLabel(), value);
103        }
104    }
105
106    @Override
107    public String marshal(T obj) throws Exception {
108        if(obj != null){
109            return labeled.cast(obj).getLabel();
110        }
111        return null;
112    }
113
114    @Override
115    public T unmarshal(String label) throws Exception {
116        if(labelMap.containsKey(label)){
117            return labelMap.get(label);
118        }
119        return null;
120    }
121}