All Downloads are FREE. Search and download functionalities are using the official Maven repository.

hudson.util.PackedMap Maven / Gradle / Ivy

The newest version!
/*******************************************************************************
 *
 * Copyright (c) 2010, CloudBees, Inc.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:       
 *
 *******************************************************************************/ 

/*
 * The MIT License
 *
 * Copyright (c) 2010, CloudBees, Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package hudson.util;

import com.thoughtworks.xstream.converters.UnmarshallingContext;
import com.thoughtworks.xstream.converters.collections.MapConverter;
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
import com.thoughtworks.xstream.mapper.Mapper;

import java.util.AbstractList;
import java.util.AbstractMap;
import java.util.AbstractSet;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

/**
 * Read-only map implementation that uses less memory than {@link HashMap}/{@link TreeMap}.
 *
 * 

* The implementation is backed by a single exact-length array, so this implementation has the following * performance characteristics. * *

    *
  • iteration is fast (but creates a lot of short-lived objects.) *
  • lookup is O(N) *
  • memory consumption is low *
* * @author Kohsuke Kawaguchi */ @SuppressWarnings({"unchecked"}) public final class PackedMap extends AbstractMap { private Object[] kvpairs; /** * * @param src * Map to copy contents from. Iteration order is preserved. */ public static PackedMap of(Map src) { return new PackedMap(src); } private PackedMap(Map src) { kvpairs = new Object[src.size()*2]; int i=0; for (Entry e : src.entrySet()) { kvpairs[i++] = e.getKey(); kvpairs[i++] = e.getValue(); } } private final Set> entrySet = new AbstractSet>() { @Override public Iterator> iterator() { return new Iterator>() { int index=0; public boolean hasNext() { return index next() { final K k = (K)kvpairs[index++]; final V v = (V)kvpairs[index++]; return new Entry() { public K getKey() { return k; } public V getValue() { return v; } public V setValue(V value) { throw new UnsupportedOperationException(); } }; } public void remove() { throw new UnsupportedOperationException(); } }; } @Override public int size() { return kvpairs.length/2; } }; @Override public Set> entrySet() { return entrySet; } @Override public boolean containsKey(Object key) { for (int i=0; i values() { return new AbstractList() { @Override public V get(int index) { return (V)kvpairs[index*2]; } @Override public int size() { return PackedMap.this.size(); } }; } /** * Should persist like a regular map. */ public static class ConverterImpl extends MapConverter { public ConverterImpl(Mapper mapper) { super(mapper); } @Override public boolean canConvert(Class type) { return type==PackedMap.class; } @Override protected Object createCollection(Class type) { return new LinkedHashMap(); } @Override public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) { return PackedMap.of((Map)super.unmarshal(reader, context)); } } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy