| 1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2007, 2018 IBM Corporation and others. |
| 3 |
* |
| 4 |
* This program and the accompanying materials |
| 5 |
* are made available under the terms of the Eclipse Public License 2.0 |
| 6 |
* which accompanies this distribution, and is available at |
| 7 |
* https://www.eclipse.org/legal/epl-2.0/ |
| 8 |
* |
| 9 |
* SPDX-License-Identifier: EPL-2.0 |
| 10 |
* |
| 11 |
* Contributors: |
| 12 |
* IBM Corporation - initial API and implementation |
| 13 |
*******************************************************************************/ |
| 14 |
package org.eclipse.equinox.p2.engine.spi; |
| 15 |
|
| 16 |
import java.util.*; |
| 17 |
|
| 18 |
/** |
| 19 |
* @since 2.0 |
| 20 |
*/ |
| 21 |
public final class Memento { |
| 22 |
private static final Collection<Class<?>> simples = Arrays.asList(String.class, Integer.class, Long.class, Float.class, Double.class, Byte.class, Short.class, Character.class, Boolean.class); |
| 23 |
private static final Collection<Class<?>> simpleArrays = Arrays.asList(String[].class, Integer[].class, Long[].class, Float[].class, Double[].class, Byte[].class, Short[].class, Character[].class, Boolean[].class); |
| 24 |
private static final Collection<Class<?>> primitiveArrays = Arrays.asList(long[].class, int[].class, short[].class, char[].class, byte[].class, double[].class, float[].class, boolean[].class); |
| 25 |
|
| 26 |
Map<String, Object> mementoMap = new HashMap<>(); |
| 27 |
|
| 28 |
public Object remove(String key) { |
| 29 |
if (key == null) |
| 30 |
throw new NullPointerException(); |
| 31 |
|
| 32 |
// TODO: persist change |
| 33 |
return mementoMap.remove(key); |
| 34 |
} |
| 35 |
|
| 36 |
public Object put(String key, Object value) { |
| 37 |
if (key == null) |
| 38 |
throw new NullPointerException(); |
| 39 |
|
| 40 |
validateValue(value); |
| 41 |
|
| 42 |
// TODO: persist change |
| 43 |
return mementoMap.put(key, value); |
| 44 |
} |
| 45 |
|
| 46 |
public Object get(String key) { |
| 47 |
if (key == null) |
| 48 |
throw new NullPointerException(); |
| 49 |
|
| 50 |
return mementoMap.get(key); |
| 51 |
} |
| 52 |
|
| 53 |
public Enumeration<String> getKeys() { |
| 54 |
return Collections.enumeration(mementoMap.keySet()); |
| 55 |
} |
| 56 |
|
| 57 |
private static void validateValue(Object value) { |
| 58 |
if (value == null) |
| 59 |
return; |
| 60 |
|
| 61 |
Class<? extends Object> clazz = value.getClass(); |
| 62 |
|
| 63 |
if (simples.contains(clazz)) |
| 64 |
return; |
| 65 |
|
| 66 |
if (simpleArrays.contains(clazz) || primitiveArrays.contains(clazz)) |
| 67 |
return; |
| 68 |
|
| 69 |
throw new IllegalArgumentException(clazz.getName()); |
| 70 |
} |
| 71 |
} |