/*
 * Copyright (c) 2011, 2021 Oracle and/or its affiliates. All rights reserved.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v. 2.0 which is available at
 * http://www.eclipse.org/legal/epl-2.0,
 * or the Eclipse Distribution License v. 1.0 which is available at
 * http://www.eclipse.org/org/documents/edl-v10.php.
 *
 * SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
 */

// Contributors:
// dmccann - December 30/2010 - 2.3 - Initial implementation
package org.eclipse.persistence.testing.jaxb.externalizedmetadata.xmladapter.list;

import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

import jakarta.xml.bind.annotation.adapters.XmlAdapter;

public final class MyListAdapter extends XmlAdapter<Object, List<String>> {
    public static String VAL0 = "00";
    public static String VAL1 = "11";
    public static String VAL2 = "22";
    public static String EMPTY_STR = "";
    public static String SPACE = " ";

    public MyListAdapter() {}

    @Override
    public List<String> unmarshal(Object arg0) throws Exception {
        List<String> list = new ArrayList<String>();
        if (arg0 instanceof Bar) {
            String idstr = ((Bar)arg0).id;
            StringTokenizer stok = new StringTokenizer(idstr, SPACE);
            while (stok.hasMoreTokens()) {
                list.add(stok.nextToken());
            }
        }
        return list;
    }

    @Override
    public Object marshal(List<String> arg0) throws Exception {
        Bar bar = new Bar();
        String id = EMPTY_STR;
        if (arg0.get(0).equals(VAL0)) {
            id += arg0.get(0);
        }
        if (arg0.get(1).equals(VAL1)) {
            id += SPACE + arg0.get(1);
        }
        if (arg0.get(2).equals(VAL2)) {
            id += SPACE + arg0.get(2);
        }
        bar.id = id;
        return bar;
    }
}
