/*******************************************************************************
 * Copyright (c) 2011 Oracle. All rights reserved.
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0 and Eclipse Distribution License v. 1.0
 * which accompanies this distribution.
 *
 * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
 * and the Eclipse Distribution License is available at
 * http://www.eclipse.org/org/documents/edl-v10.php.
 *
 * Contributors:
 *  - rbarkhouse - 26 October 2011 - 2.4 - Initial implementation
 ******************************************************************************/
package org.eclipse.persistence.testing.jaxb.idresolver;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.concurrent.Callable;

import javax.xml.bind.ValidationEventHandler;

import org.eclipse.persistence.jaxb.IDResolver;

import org.xml.sax.SAXException;

public class MyIDResolver extends IDResolver {
    Map<String, Apple> apples = new LinkedHashMap<String, Apple>();
    Map<String, Orange> oranges = new LinkedHashMap<String, Orange>();

    public boolean hitStartDocument = false;
    public boolean hitEndDocument = false;
    public boolean hitBind = false;
    public boolean hitResolve = false;
    public boolean hitBindSingle = false;
    public boolean hitResolveSingle = false;
    public boolean eventHandlerNotNull = false;

    @Override
    public void startDocument(ValidationEventHandler eventHandler) throws SAXException {
        apples.clear();
        oranges.clear();
        hitStartDocument = true;
        eventHandlerNotNull = (eventHandler != null);
    }

    @Override
    public void endDocument() throws SAXException {
        hitEndDocument = true;
    }

    @Override
    public void bind(Map<String, Object> idWrapper, Object obj) throws SAXException {
        hitBind = true;
        String id = idWrapper.values().toArray()[0].toString();
        if (obj instanceof Apple) {
            ((Apple) obj).processed = true;
            apples.put(id, (Apple) obj);
        } else {
            ((Orange) obj).processed = true;
            oranges.put(id, (Orange) obj);
        }
    }

    @Override
    public Callable<Object> resolve(Map<String, Object> idWrapper, final Class type) throws SAXException {
        hitResolve = true;
        final String id = idWrapper.values().toArray()[0].toString();
        return new Callable<Object>() {
            public Object call() {
                if (type == Apple.class) {
                    return apples.get(id);
                } else {
                    return oranges.get(id);
                }
            }
        };
    }

    @Override
    public void bind(Object id, Object obj) throws SAXException {
        hitBindSingle = true;
        HashMap<String, Object> idMap = new HashMap<String, Object>(1);
        idMap.put("stringId", id);
        bind(idMap, obj);
    }

    @Override
    public Callable<?> resolve(Object id, Class type) throws SAXException {
        hitResolveSingle = true;
        HashMap<String, Object> idMap = new HashMap<String, Object>(1);
        idMap.put("stringId", id);
        return resolve(idMap, type);
    }

}
@@ -1073,24 +1073,28 @@
 lver %7B%0D%0A    
+Map%3C
 Map%3CString, 
@@ -1088,24 +1088,33 @@
 %3CMap%3CString,
+ Object%3E,
  Apple%3E appl
@@ -1139,23 +1139,8 @@
 hMap
-%3CString, Apple%3E
 ();%0D
@@ -1140,24 +1140,28 @@
 Map();%0D%0A    
+Map%3C
 Map%3CString, 
@@ -1151,32 +1151,41 @@
  Map%3CMap%3CString,
+ Object%3E,
  Orange%3E oranges
@@ -1208,24 +1208,8 @@
 hMap
-%3CString, Orange%3E
 ();%0D
@@ -2012,73 +2012,8 @@
 e;%0D%0A
-        String id = idWrapper.values().toArray()%5B0%5D.toString();%0D%0A
     
@@ -2115,16 +2115,23 @@
 s.put(id
+Wrapper
 , (Apple
@@ -2229,16 +2229,23 @@
 s.put(id
+Wrapper
 , (Orang
@@ -2325,16 +2325,22 @@
 resolve(
+final 
 Map%3CStri
@@ -2435,79 +2435,8 @@
 e;%0D%0A
-        final String id = idWrapper.values().toArray()%5B0%5D.toString();%0D%0A
     
@@ -2584,32 +2584,39 @@
 rn apples.get(id
+Wrapper
 );%0D%0A            
@@ -2670,16 +2670,23 @@
 s.get(id
+Wrapper
 );%0D%0A    
@@ -2849,36 +2849,32 @@
  true;%0D%0A        
-Hash
 Map%3CString, Obje
@@ -2881,32 +2881,38 @@
 ct%3E idMap = new 
+Linked
 HashMap%3CString, 
@@ -3130,20 +3130,16 @@
         
-Hash
 Map%3CStri
@@ -3162,16 +3162,22 @@
 p = new 
+Linked
 HashMap%3C

Fix for bug 365929, JDK1.5 test failures