Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright (c) 2012, Codename One and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Codename One designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Codename One through http://www.codenameone.com/ if you
* need additional information or have any questions.
*/
package com.codename1.io;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
/**
*
Simple map like class to store application and Codename One preference
* settings in the {@link com.codename1.io.Storage}.
* Simple usage of the class for storing a {@code String} token:
*
*
*
*
* Notice that this class might get somewhat confusing with primitive numbers e.g. if you use
* {@code Preferences.set("primitiveLongValue", myLongNumber)} then invoke
* {@code Preferences.get("primitiveLongValue", 0)} you might get an exception!
* This would happen because the value is physically a {@code Long} object but you are trying to get an
* {@code Integer}.
* The workaround is to remain consistent and use code like this {@code Preferences.get("primitiveLongValue", (long)0)}.
*
*
* @author Shai Almog
* @author Miguel Mu\u00f1oz
*/
public class Preferences {
private static Hashtable p;
private static final HashMap> listenerMap = new HashMap>();
private static String preferencesLocation = "CN1Preferences";
/**
* Block instantiation of preferences
*/
Preferences() {}
/**
* Sets the location within the storage of the preferences file to an arbitrary name. This is useful in a case
* of encryption where we would want preferences to use a different file name.
*
* @param storageFileName the name of the preferences file
*/
public static void setPreferencesLocation(String storageFileName) {
preferencesLocation = storageFileName;
p = null;
}
/**
* Returns the location within the storage of the preferences file to an arbitrary name. This is useful in a case
* of encryption where we would want preferences to use a different file name.
* @return the storage file name
*/
public static String getPreferencesLocation() {
return preferencesLocation;
}
private synchronized static Hashtable get() {
if(p == null) {
if(Storage.getInstance().exists(preferencesLocation)) {
p = (Hashtable)Storage.getInstance().readObject(preferencesLocation);
}
if(p == null) {
p = new Hashtable();
}
}
return p;
}
private static synchronized void save() {
Storage.getInstance().writeObject(preferencesLocation, p);
}
/**
* Sets a preference value, supported values are Strings, numbers and boolean
*
* @param pref the key any unique none null value that doesn't start with cn1
* @param o a String a number or boolean
*/
private static void set(String pref, Object o) {
Object prior = get(pref, null);
if(o == null) {
get().remove(pref);
} else {
get().put(pref, o);
}
save();
fireChange(pref, prior, o);
}
/**
* Sets a set of preference values as a batch, and performs a single save.
* @param values The key/value pairs to set in preferences.
*/
public static void set(Map values) {
ArrayList