diff --git a/core/org.eclipse.cdt.core.tests/misc/org/eclipse/cdt/core/internal/tests/BaseOptionTest.java b/core/org.eclipse.cdt.core.tests/misc/org/eclipse/cdt/core/internal/tests/BaseOptionTest.java new file mode 100644 index 00000000000..273f552caae --- /dev/null +++ b/core/org.eclipse.cdt.core.tests/misc/org/eclipse/cdt/core/internal/tests/BaseOptionTest.java @@ -0,0 +1,58 @@ +/******************************************************************************* + * Copyright (c) 2020 ArSysOp and others. + * + * This program and the accompanying materials + * are made available under the terms of the Eclipse Public License 2.0 + * which accompanies this distribution, and is available at + * https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * Alexander Fedorov - Initial API and implementation + *******************************************************************************/ +package org.eclipse.cdt.core.internal.tests; + +import static org.junit.Assert.assertEquals; + +import org.eclipse.cdt.core.options.BaseOption; +import org.junit.Test; + +public class BaseOptionTest { + + @Test + public void testBaseOption() { + BaseOption option = new BaseOption<>(String.class, "identifier", "default", "name", "description"); + assertEquals("identifier", option.identifer()); + assertEquals("default", option.defaultValue()); + assertEquals("name", option.name()); + assertEquals("description", option.description()); + assertEquals(String.class, option.valueClass()); + } + + @Test(expected = NullPointerException.class) + public void testBaseOptionNullValueType() { + new BaseOption<>(null, "identifier", "default", "name", "description"); + } + + @Test(expected = NullPointerException.class) + public void testBaseOptionNullIdentifier() { + new BaseOption<>(Object.class, null, "default", "name", "description"); + } + + @Test(expected = NullPointerException.class) + public void testBaseOptionNullDefaultValue() { + new BaseOption<>(Object.class, "identifier", null, "name", "description"); + } + + @Test(expected = NullPointerException.class) + public void testBaseOptionNullName() { + new BaseOption<>(Object.class, "identifier", "default", null, "description"); + } + + @Test(expected = NullPointerException.class) + public void testBaseOptionNullDescription() { + new BaseOption<>(Object.class, "identifier", "default", "name", null); + } + +} diff --git a/core/org.eclipse.cdt.core.tests/misc/org/eclipse/cdt/core/internal/tests/OsgiPreferenceStorageTest.java b/core/org.eclipse.cdt.core.tests/misc/org/eclipse/cdt/core/internal/tests/OsgiPreferenceStorageTest.java new file mode 100644 index 00000000000..40c488ae898 --- /dev/null +++ b/core/org.eclipse.cdt.core.tests/misc/org/eclipse/cdt/core/internal/tests/OsgiPreferenceStorageTest.java @@ -0,0 +1,195 @@ +/******************************************************************************* + * Copyright (c) 2020 ArSysOp and others. + * + * This program and the accompanying materials + * are made available under the terms of the Eclipse Public License 2.0 + * which accompanies this distribution, and is available at + * https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * Alexander Fedorov - Initial API and implementation + *******************************************************************************/ +package org.eclipse.cdt.core.internal.tests; + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import org.eclipse.cdt.core.options.BaseOption; +import org.eclipse.cdt.core.options.OsgiPreferenceStorage; +import org.junit.Test; +import org.osgi.service.prefs.Preferences; + +public class OsgiPreferenceStorageTest { + + private final BaseOption unknown = new BaseOption<>(Object.class, "unknown", "", "Unknown", + "An option with unclear semantic, i.e. typical one"); + private final BaseOption string2020 = new BaseOption<>(String.class, "string2020", "2020", "String 2020"); + private final BaseOption negative = new BaseOption<>(Boolean.class, "negative", false, "Negative"); + private final BaseOption positive = new BaseOption<>(Boolean.class, "positive", true, "Positive"); + private final BaseOption bytes2020 = new BaseOption<>(byte[].class, "bytes2020", new byte[] { 20, 20 }, + "Bytes 2020"); + private final BaseOption double2020 = new BaseOption<>(Double.class, "double2020", 2020d, "Double 2020"); + private final BaseOption float2020 = new BaseOption<>(Float.class, "float2020", 2020f, "Float 2020"); + private final BaseOption int2020 = new BaseOption<>(Integer.class, "int2020", 2020, "Int 2020"); + private final BaseOption long2020 = new BaseOption<>(Long.class, "long2020", 2020l, "Long 2020"); + + @Test(expected = NullPointerException.class) + public void testNullPreferences() { + OsgiPreferenceStorage storage = new OsgiPreferenceStorage(null); + } + + @Test + public void testConsumable() { + Preferences preferences = anyPreferences(); + OsgiPreferenceStorage storage = new OsgiPreferenceStorage(preferences); + assertFalse(storage.consumable(Object.class)); + assertTrue(storage.consumable(String.class)); + assertTrue(storage.consumable(Boolean.class)); + assertTrue(storage.consumable(byte[].class)); + assertFalse(storage.consumable(Byte[].class)); + assertTrue(storage.consumable(Double.class)); + assertTrue(storage.consumable(Float.class)); + assertTrue(storage.consumable(Integer.class)); + assertTrue(storage.consumable(Long.class)); + } + + @Test + public void testLoadString() { + Preferences preferences = anyPreferences(); + OsgiPreferenceStorage storage = new OsgiPreferenceStorage(preferences); + preferences.put(string2020.identifer(), "2002"); + assertEquals("2002", storage.load(string2020)); + } + + @Test + public void testLoadBoolean() { + Preferences preferences = anyPreferences(); + OsgiPreferenceStorage storage = new OsgiPreferenceStorage(preferences); + preferences.putBoolean(negative.identifer(), true); + assertEquals(true, storage.load(negative)); + preferences.putBoolean(positive.identifer(), false); + assertEquals(false, storage.load(positive)); + } + + @Test + public void testLoadByteArray() { + Preferences preferences = anyPreferences(); + OsgiPreferenceStorage storage = new OsgiPreferenceStorage(preferences); + preferences.putByteArray(bytes2020.identifer(), new byte[] { 20, 02 }); + assertArrayEquals(new byte[] { 20, 02 }, storage.load(bytes2020)); + } + + @Test + public void testLoadDouble() { + Preferences preferences = anyPreferences(); + OsgiPreferenceStorage storage = new OsgiPreferenceStorage(preferences); + preferences.putDouble(double2020.identifer(), 2002d); + assertEquals(2002d, storage.load(double2020), 0); + } + + @Test + public void testLoadFloat() { + Preferences preferences = anyPreferences(); + OsgiPreferenceStorage storage = new OsgiPreferenceStorage(preferences); + preferences.putFloat(float2020.identifer(), 2002f); + assertEquals(2002f, storage.load(float2020), 0); + } + + @Test + public void testLoadInt() { + Preferences preferences = anyPreferences(); + OsgiPreferenceStorage storage = new OsgiPreferenceStorage(preferences); + preferences.putLong(int2020.identifer(), 2002l); + assertEquals(2002l, (long) storage.load(int2020)); + } + + @Test + public void testLoadLong() { + Preferences preferences = anyPreferences(); + OsgiPreferenceStorage storage = new OsgiPreferenceStorage(preferences); + preferences.putLong(long2020.identifer(), 2002l); + assertEquals(2002l, (long) storage.load(long2020)); + } + + @Test(expected = UnsupportedOperationException.class) + public void testLoadUnknown() { + Preferences preferences = anyPreferences(); + OsgiPreferenceStorage storage = new OsgiPreferenceStorage(preferences); + assertEquals(new Object(), storage.load(unknown)); + } + + @Test + public void testSaveString() { + Preferences anyPreferences = anyPreferences(); + OsgiPreferenceStorage storage = new OsgiPreferenceStorage(anyPreferences); + storage.save("2002", string2020); + assertEquals("2002", anyPreferences.get(string2020.identifer(), string2020.defaultValue())); + } + + @Test + public void testSaveBoolean() { + Preferences anyPreferences = anyPreferences(); + OsgiPreferenceStorage storage = new OsgiPreferenceStorage(anyPreferences); + storage.save(true, negative); + assertEquals(true, anyPreferences.getBoolean(negative.identifer(), negative.defaultValue())); + storage.save(false, positive); + assertEquals(false, anyPreferences.getBoolean(positive.identifer(), positive.defaultValue())); + } + + @Test + public void testSaveByteArray() { + Preferences anyPreferences = anyPreferences(); + OsgiPreferenceStorage storage = new OsgiPreferenceStorage(anyPreferences); + storage.save(new byte[] { 20, 02 }, bytes2020); + assertArrayEquals(new byte[] { 20, 02 }, + anyPreferences.getByteArray(bytes2020.identifer(), bytes2020.defaultValue())); + } + + @Test + public void testSaveDouble() { + Preferences preferences = anyPreferences(); + OsgiPreferenceStorage storage = new OsgiPreferenceStorage(preferences); + storage.save(2002d, double2020); + assertEquals(2002d, preferences.getDouble(double2020.identifer(), double2020.defaultValue()), 0); + } + + @Test + public void testSaveFloat() { + Preferences preferences = anyPreferences(); + OsgiPreferenceStorage storage = new OsgiPreferenceStorage(preferences); + storage.save(2002f, float2020); + assertEquals(2002f, preferences.getDouble(float2020.identifer(), float2020.defaultValue()), 0); + } + + @Test + public void testSaveInt() { + Preferences preferences = anyPreferences(); + OsgiPreferenceStorage storage = new OsgiPreferenceStorage(preferences); + storage.save(2002, int2020); + assertEquals(2002, preferences.getInt(int2020.identifer(), int2020.defaultValue())); + } + + @Test + public void testSaveLong() { + Preferences preferences = anyPreferences(); + OsgiPreferenceStorage storage = new OsgiPreferenceStorage(preferences); + storage.save(2002l, long2020); + assertEquals(2002l, preferences.getLong(long2020.identifer(), long2020.defaultValue())); + } + + @Test(expected = UnsupportedOperationException.class) + public void testSaveUnknown() { + Preferences preferences = anyPreferences(); + OsgiPreferenceStorage storage = new OsgiPreferenceStorage(preferences); + storage.save(new Object(), unknown); + } + + private Preferences anyPreferences() { + return new org.eclipse.core.internal.preferences.EclipsePreferences(); + } + +} diff --git a/core/org.eclipse.cdt.core.tests/suite/org/eclipse/cdt/core/suite/AutomatedIntegrationSuite.java b/core/org.eclipse.cdt.core.tests/suite/org/eclipse/cdt/core/suite/AutomatedIntegrationSuite.java index 6babc089cbd..cfa0142ad91 100644 --- a/core/org.eclipse.cdt.core.tests/suite/org/eclipse/cdt/core/suite/AutomatedIntegrationSuite.java +++ b/core/org.eclipse.cdt.core.tests/suite/org/eclipse/cdt/core/suite/AutomatedIntegrationSuite.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2005, 2015 IBM Corporation and others. + * Copyright (c) 2005, 2020 IBM Corporation and others. * * This program and the accompanying materials * are made available under the terms of the Eclipse Public License 2.0 @@ -12,6 +12,7 @@ * IBM Corporation - initial API and implementation * Markus Schorn (Wind River Systems) * Norbert Ploett (Siemens AG) + * Alexander Fedorov Bug 559193 *******************************************************************************/ package org.eclipse.cdt.core.suite; @@ -20,6 +21,8 @@ import org.eclipse.cdt.core.cdescriptor.tests.CDescriptorTests; import org.eclipse.cdt.core.envvar.IEnvironmentVariableManagerTests; import org.eclipse.cdt.core.internal.efsextension.tests.EFSExtensionTests; import org.eclipse.cdt.core.internal.errorparsers.tests.ErrorParserTests; +import org.eclipse.cdt.core.internal.tests.BaseOptionTest; +import org.eclipse.cdt.core.internal.tests.OsgiPreferenceStorageTest; import org.eclipse.cdt.core.internal.tests.PositionTrackerTests; import org.eclipse.cdt.core.internal.tests.ResourceLookupTests; import org.eclipse.cdt.core.internal.tests.StringBuilderTest; @@ -43,6 +46,7 @@ import org.eclipse.cdt.utils.UNCPathConverterTest; import org.eclipse.cdt.utils.WeakHashSetTest; import org.eclipse.cdt.utils.elf.ElfParserTest; +import junit.framework.JUnit4TestAdapter; import junit.framework.Test; import junit.framework.TestCase; import junit.framework.TestSuite; @@ -82,6 +86,8 @@ public class AutomatedIntegrationSuite extends TestSuite { suite.addTest(AllCoreTests.suite()); suite.addTest(ElementDeltaTests.suite()); suite.addTest(WorkingCopyTests.suite()); + suite.addTest(new JUnit4TestAdapter(BaseOptionTest.class)); + suite.addTest(new JUnit4TestAdapter(OsgiPreferenceStorageTest.class)); suite.addTest(PositionTrackerTests.suite()); suite.addTest(ResourceLookupTests.suite()); suite.addTest(StringBuilderTest.suite()); diff --git a/core/org.eclipse.cdt.core/.classpath b/core/org.eclipse.cdt.core/.classpath index 8672427f779..51ccf9ff55a 100644 --- a/core/org.eclipse.cdt.core/.classpath +++ b/core/org.eclipse.cdt.core/.classpath @@ -8,5 +8,6 @@ + diff --git a/core/org.eclipse.cdt.core/META-INF/MANIFEST.MF b/core/org.eclipse.cdt.core/META-INF/MANIFEST.MF index 44eebcb7a7c..049200245a2 100644 --- a/core/org.eclipse.cdt.core/META-INF/MANIFEST.MF +++ b/core/org.eclipse.cdt.core/META-INF/MANIFEST.MF @@ -33,6 +33,7 @@ Export-Package: org.eclipse.cdt.core, org.eclipse.cdt.core.language.settings.providers, org.eclipse.cdt.core.model, org.eclipse.cdt.core.model.util, + org.eclipse.cdt.core.options;x-friends:="org.eclipse.cdt.ui", org.eclipse.cdt.core.parser, org.eclipse.cdt.core.parser.ast, org.eclipse.cdt.core.parser.util, @@ -81,6 +82,7 @@ Export-Package: org.eclipse.cdt.core, org.eclipse.cdt.debug.ui, org.eclipse.cdt.codan.ui", org.eclipse.cdt.internal.core.model.ext;x-friends:="org.eclipse.cdt.ui", + org.eclipse.cdt.internal.core.options;x-friends:="org.eclipse.cdt.ui", org.eclipse.cdt.internal.core.parser;x-internal:=true, org.eclipse.cdt.internal.core.parser.problem;x-internal:=true, org.eclipse.cdt.internal.core.parser.scanner;x-friends:="org.eclipse.cdt.ui", diff --git a/core/org.eclipse.cdt.core/build.properties b/core/org.eclipse.cdt.core/build.properties index dbcd2b3fb2f..bade6f71b65 100644 --- a/core/org.eclipse.cdt.core/build.properties +++ b/core/org.eclipse.cdt.core/build.properties @@ -32,4 +32,5 @@ source.. = src/,\ parser/,\ browser/,\ templateengine/,\ + options/,\ utils/ diff --git a/core/org.eclipse.cdt.core/options/org/eclipse/cdt/core/options/BaseOption.java b/core/org.eclipse.cdt.core/options/org/eclipse/cdt/core/options/BaseOption.java new file mode 100644 index 00000000000..119bcc85271 --- /dev/null +++ b/core/org.eclipse.cdt.core/options/org/eclipse/cdt/core/options/BaseOption.java @@ -0,0 +1,99 @@ +/******************************************************************************* + * Copyright (c) 2020 ArSysOp and others. + * + * This program and the accompanying materials + * are made available under the terms of the Eclipse Public License 2.0 + * which accompanies this distribution, and is available at + * https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * Alexander Fedorov - Initial API and implementation + *******************************************************************************/ +package org.eclipse.cdt.core.options; + +import java.util.Objects; + +import org.eclipse.cdt.internal.core.options.OptionMessages; + +/** + * The base implementation for option metadata + * + * @param the value type for the option + * + * @see OptionMetadata + * @see OptionStorage + */ +public final class BaseOption implements OptionMetadata { + + private final Class clazz; + private final String identifier; + private final V defaultValue; + private final String name; + private final String description; + + /** + * Created an instance of BaseOption using name as description + * + * @param clazz the value type of the option, must not be null + * @param identifier the identifier of the option, must not be null + * @param defaultValue the default value of the option, must not be null + * @param name the name of the option, must not be null + * + * @see BaseOption#BaseOption(Class, String, Object, String, String) + */ + public BaseOption(Class clazz, String identifier, V defaultValue, String name) { + this(clazz, identifier, defaultValue, name, name); + } + + /** + * Created an instance of BaseOption of all the the given parameters + * + * @param clazz the value type of the option, must not be null + * @param identifier the identifier of the option, must not be null + * @param defaultValue the default value of the option, must not be null + * @param name the name of the option, must not be null + * @param description the description of the option, must not be null + * + * @see BaseOption#BaseOption(Class, String, Object, String) + */ + public BaseOption(Class clazz, String identifier, V defaultValue, String name, String description) { + Objects.requireNonNull(clazz, OptionMessages.BaseOption_e_null_value_type); + Objects.requireNonNull(identifier, OptionMessages.BaseOption_e_null_identifier); + Objects.requireNonNull(defaultValue, OptionMessages.BaseOption_e_null_default_value); + Objects.requireNonNull(name, OptionMessages.BaseOption_e_null_name); + Objects.requireNonNull(description, OptionMessages.BaseOption_e_null_description); + this.clazz = clazz; + this.identifier = identifier; + this.defaultValue = defaultValue; + this.name = name; + this.description = description; + } + + @Override + public String identifer() { + return identifier; + } + + @Override + public V defaultValue() { + return defaultValue; + } + + @Override + public String name() { + return name; + } + + @Override + public String description() { + return description; + } + + @Override + public Class valueClass() { + return clazz; + } + +} diff --git a/core/org.eclipse.cdt.core/options/org/eclipse/cdt/core/options/OptionMetadata.java b/core/org.eclipse.cdt.core/options/org/eclipse/cdt/core/options/OptionMetadata.java new file mode 100644 index 00000000000..b2c2dd7f815 --- /dev/null +++ b/core/org.eclipse.cdt.core/options/org/eclipse/cdt/core/options/OptionMetadata.java @@ -0,0 +1,61 @@ +/******************************************************************************* + * Copyright (c) 2020 ArSysOp and others. + * + * This program and the accompanying materials + * are made available under the terms of the Eclipse Public License 2.0 + * which accompanies this distribution, and is available at + * https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * Alexander Fedorov - Initial API and implementation + *******************************************************************************/ +package org.eclipse.cdt.core.options; + +/** + * The option metadata provides the information needed to configure + * everything about the option except the option value itself. + * + * @param the value type for the option + */ +public interface OptionMetadata { + + /** + * The option identifier to use as a key to access the option value. + * Must not be null. + * + * @return the identifier + */ + String identifer(); + + /** + * The default value for the option. Must not be null. + * + * @return the option's default value + */ + V defaultValue(); + + /** + * Briefly describes the option purpose, intended to be used in UI. + * Must not be null and should be localized. Should not be blank. + * + * @return the option's name + */ + String name(); + + /** + * Widely describes the option purpose, intended to be used in UI. + * Must not be null and should be localized. May be blank. + * + * @return the option's description + */ + String description(); + + /** + * The type of option value needed to perform type checks. Must not be null. + * + * @return the value class + */ + Class valueClass(); +} diff --git a/core/org.eclipse.cdt.core/options/org/eclipse/cdt/core/options/OptionStorage.java b/core/org.eclipse.cdt.core/options/org/eclipse/cdt/core/options/OptionStorage.java new file mode 100644 index 00000000000..b724d4d5394 --- /dev/null +++ b/core/org.eclipse.cdt.core/options/org/eclipse/cdt/core/options/OptionStorage.java @@ -0,0 +1,62 @@ +/******************************************************************************* + * Copyright (c) 2020 ArSysOp and others. + * + * This program and the accompanying materials + * are made available under the terms of the Eclipse Public License 2.0 + * which accompanies this distribution, and is available at + * https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * Alexander Fedorov - Initial API and implementation + *******************************************************************************/ +package org.eclipse.cdt.core.options; + +/** + * Provides metadata-based access to an enclosed storage of options. + * + */ +public interface OptionStorage { + + /** + * Checks if the value type can be consumed by an enclosed storage. + * + * @param the value type for the option + * @param valueType the value type to be checked + * + * @return true if this value type can be consumed by an enclosed storage and false otherwise + */ + boolean consumable(Class valueType); + + /** + * Loads the value of specified option from an enclosed storage. + * If the value is not found returns the option default value. + * + * @param the value type for the option + * @param option the option metadata, must not be null. + * + * @return the option value or default value if option is unknown + * @throws UnsupportedOperationException for unsupported option value types + * + * @see #consumable(Class) + * @see OptionMetadata + */ + V load(OptionMetadata option); + + /** + * Saves the value of specified option to the enclosed storage. + * + * @param the value type for the option + * @param value to be saved, must not be null. + * @param option the option metadata, must not be null. + * + * @return the option value or default value if option is unknown + * @throws UnsupportedOperationException for unsupported option value types + * + * @see #consumable(Class) + * @see OptionMetadata + */ + void save(V value, OptionMetadata option); + +} diff --git a/core/org.eclipse.cdt.core/options/org/eclipse/cdt/core/options/OsgiPreferenceStorage.java b/core/org.eclipse.cdt.core/options/org/eclipse/cdt/core/options/OsgiPreferenceStorage.java new file mode 100644 index 00000000000..94794f6034e --- /dev/null +++ b/core/org.eclipse.cdt.core/options/org/eclipse/cdt/core/options/OsgiPreferenceStorage.java @@ -0,0 +1,105 @@ +/******************************************************************************* + * Copyright (c) 2020 ArSysOp and others. + * + * This program and the accompanying materials + * are made available under the terms of the Eclipse Public License 2.0 + * which accompanies this distribution, and is available at + * https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * Alexander Fedorov - Initial API and implementation + *******************************************************************************/ +package org.eclipse.cdt.core.options; + +import java.util.HashSet; +import java.util.Objects; +import java.util.Set; + +import org.eclipse.cdt.internal.core.options.OptionMessages; +import org.eclipse.osgi.util.NLS; +import org.osgi.service.prefs.Preferences; + +/** + * The option storage implementation that uses OSGi preference node as an enclosed storage. + * + * @see Preferences + * + */ +public final class OsgiPreferenceStorage implements OptionStorage { + + private final Preferences preferences; + private final Set> classes; + + /** + * + * @param preferences the OSGi preference node, must not be null + */ + public OsgiPreferenceStorage(Preferences preferences) { + Objects.requireNonNull(preferences, OptionMessages.OsgiPreferenceStorage_e_null_preference_node); + this.preferences = preferences; + this.classes = new HashSet<>(); + classes.add(String.class); + classes.add(Boolean.class); + classes.add(byte[].class); + classes.add(Double.class); + classes.add(Float.class); + classes.add(Integer.class); + classes.add(Long.class); + } + + @Override + public boolean consumable(Class valueType) { + return classes.contains(valueType); + } + + @Override + public V load(OptionMetadata option) { + Class valueClass = option.valueClass(); + String identifer = option.identifer(); + V defaultValue = option.defaultValue(); + if (String.class.equals(valueClass)) { + return valueClass.cast(preferences.get(identifer, String.class.cast(defaultValue))); + } else if (Boolean.class.equals(valueClass)) { + return valueClass.cast(preferences.getBoolean(identifer, Boolean.class.cast(defaultValue))); + } else if (byte[].class.equals(valueClass)) { + return valueClass.cast(preferences.getByteArray(identifer, byte[].class.cast(defaultValue))); + } else if (Double.class.equals(valueClass)) { + return valueClass.cast(preferences.getDouble(identifer, Double.class.cast(defaultValue))); + } else if (Float.class.equals(valueClass)) { + return valueClass.cast(preferences.getFloat(identifer, Float.class.cast(defaultValue))); + } else if (Integer.class.equals(valueClass)) { + return valueClass.cast(preferences.getInt(identifer, Integer.class.cast(defaultValue))); + } else if (Long.class.equals(valueClass)) { + return valueClass.cast(preferences.getLong(identifer, Long.class.cast(defaultValue))); + } + String message = OptionMessages.PreferenceStorage_e_load_unsupported; + throw new UnsupportedOperationException(NLS.bind(message, option, valueClass)); + } + + @Override + public void save(V value, OptionMetadata option) { + Class valueClass = option.valueClass(); + String identifer = option.identifer(); + if (String.class.equals(valueClass)) { + preferences.put(identifer, String.class.cast(value)); + } else if (Boolean.class.equals(valueClass)) { + preferences.putBoolean(identifer, Boolean.class.cast(value)); + } else if (byte[].class.equals(valueClass)) { + preferences.putByteArray(identifer, byte[].class.cast(value)); + } else if (Double.class.equals(valueClass)) { + preferences.putDouble(identifer, Double.class.cast(value)); + } else if (Float.class.equals(valueClass)) { + preferences.putFloat(identifer, Float.class.cast(value)); + } else if (Integer.class.equals(valueClass)) { + preferences.putInt(identifer, Integer.class.cast(value)); + } else if (Long.class.equals(valueClass)) { + preferences.putLong(identifer, Long.class.cast(value)); + } else { + String message = OptionMessages.PreferenceStorage_e_save_unsupported; + throw new UnsupportedOperationException(NLS.bind(message, option, valueClass)); + } + } + +} diff --git a/core/org.eclipse.cdt.core/options/org/eclipse/cdt/internal/core/options/OptionMessages.java b/core/org.eclipse.cdt.core/options/org/eclipse/cdt/internal/core/options/OptionMessages.java new file mode 100644 index 00000000000..cedc61f2813 --- /dev/null +++ b/core/org.eclipse.cdt.core/options/org/eclipse/cdt/internal/core/options/OptionMessages.java @@ -0,0 +1,35 @@ +/******************************************************************************* + * Copyright (c) 2020 ArSysOp and others. + * + * This program and the accompanying materials + * are made available under the terms of the Eclipse Public License 2.0 + * which accompanies this distribution, and is available at + * https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * Alexander Fedorov - Initial API and implementation + *******************************************************************************/ +package org.eclipse.cdt.internal.core.options; + +import org.eclipse.osgi.util.NLS; + +public class OptionMessages extends NLS { + private static final String BUNDLE_NAME = "org.eclipse.cdt.internal.core.options.OptionMessages"; //$NON-NLS-1$ + public static String BaseOption_e_null_default_value; + public static String BaseOption_e_null_description; + public static String BaseOption_e_null_identifier; + public static String BaseOption_e_null_name; + public static String BaseOption_e_null_value_type; + public static String OsgiPreferenceStorage_e_null_preference_node; + public static String PreferenceStorage_e_load_unsupported; + public static String PreferenceStorage_e_save_unsupported; + static { + // initialize resource bundle + NLS.initializeMessages(BUNDLE_NAME, OptionMessages.class); + } + + private OptionMessages() { + } +} diff --git a/core/org.eclipse.cdt.core/options/org/eclipse/cdt/internal/core/options/OptionMessages.properties b/core/org.eclipse.cdt.core/options/org/eclipse/cdt/internal/core/options/OptionMessages.properties new file mode 100644 index 00000000000..1b370c4d3d6 --- /dev/null +++ b/core/org.eclipse.cdt.core/options/org/eclipse/cdt/internal/core/options/OptionMessages.properties @@ -0,0 +1,22 @@ +############################################################################### +# Copyright (c) 2020 ArSysOp and others. +# +# This program and the accompanying materials +# are made available under the terms of the Eclipse Public License 2.0 +# which accompanies this distribution, and is available at +# https://www.eclipse.org/legal/epl-2.0/ +# +# SPDX-License-Identifier: EPL-2.0 +# +# Contributors: +# Alexander Fedorov - Initial implementation +############################################################################### + +BaseOption_e_null_default_value=Option default value must not be null +BaseOption_e_null_description=Option description must not be null +BaseOption_e_null_identifier=Option identifier must not be null +BaseOption_e_null_name=Option name must not be null +BaseOption_e_null_value_type=Option value type must not be null +OsgiPreferenceStorage_e_null_preference_node=Preference node must not be null +PreferenceStorage_e_load_unsupported=Failed to load value for option {0}: class {1} is unsupported +PreferenceStorage_e_save_unsupported=Failed to save value for option {0}: class {1} is unsupported