org.kie.internal.jaxb.StringKeyObjectValueMap Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of kie-internal Show documentation
Show all versions of kie-internal Show documentation
The Drools and jBPM internal API which is NOT backwards compatible between releases.
/*
* Copyright 2015 Red Hat, Inc. and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.kie.internal.jaxb;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;
import static org.kie.internal.jaxb.StringKeyObjectValueMapXmlAdapter.createJaxbStringObjectMapEntry;
import static org.kie.internal.jaxb.StringKeyObjectValueMapXmlAdapter.deserializeObject;
/**
* This implements {@link Map} in order to fool JSON..
*/
@XmlType
@XmlAccessorType(XmlAccessType.FIELD)
public class StringKeyObjectValueMap implements Map {
@XmlElement(name="entry")
public List entries = new ArrayList<>();
public void addEntry(StringKeyObjectValueEntry newEntry) {
this.entries.add(newEntry);
}
@Override
public int size() {
return entries.size();
}
@Override
public boolean isEmpty() {
return entries.isEmpty();
}
@Override
public boolean containsKey(Object key) {
if( key == null ) {
return false;
}
for( StringKeyObjectValueEntry entry : entries ) {
if( key.equals(entry.getKey())) {
return true;
}
}
return false;
}
@Override
public boolean containsValue(Object value) {
if( value == null ) {
return false;
}
for( StringKeyObjectValueEntry entry : entries ) {
Object entryVal = deserializeObject(entry.getBytes(), entry.getClassName(), entry.getKey());
if( value.equals(entryVal) ) {
return true;
}
}
return false;
}
@Override
public Object get(Object key) {
if( key == null ) {
return null;
}
for( StringKeyObjectValueEntry entry : entries ) {
if( key != null && key.equals(entry.getKey())) {
return deserializeObject(entry.getBytes(), entry.getClassName(), key.toString());
}
}
return null;
}
@Override
public Object put(String key, Object value) {
Object oldVal = get(key);
StringKeyObjectValueEntry newEntry = createJaxbStringObjectMapEntry(value, key);
entries.add(newEntry);
return oldVal;
}
@Override
public Object remove(Object key) {
Iterator iter = entries.iterator();
while( iter.hasNext() ) {
StringKeyObjectValueEntry entry = iter.next();
String entryKey = entry.getKey();
if( key.equals(entryKey) ) {
iter.remove();
return deserializeObject(entry.getBytes(), entry.getClassName(), entry.getKey());
}
}
return null;
}
@Override
public void putAll(Map extends String, ? extends Object> m) {
for( Entry, ?> entry : m.entrySet() ) {
StringKeyObjectValueEntry newEntry = createJaxbStringObjectMapEntry(entry.getValue(), entry.getKey().toString());
entries.add(newEntry);
}
}
@Override
public void clear() {
entries.clear();
}
@Override
public Set keySet() {
Set keySet = new HashSet<>();
for( StringKeyObjectValueEntry entry : entries ) {
keySet.add(entry.getKey());
}
return keySet;
}
@Override
public Collection