Type safety in enum wrapper - java

Background info:
Sometimes you need to share a couple of global preferences in your android application and one option is to use the SharedPreferences to accomplish this;
//get the preferences
SharedPreferences prefs = myActivity().getSharedPreferences(“ConfigurationStore”, Context.MODE_PRIVATE);
//store a value
prefs.edit().putString(“user”, “Teddy”).commit();
//get the value
prefs.getString(“user”, null);
I like my code simple so I wrote a wrapper to hide the above, here is the result.
public enum ConfigurationStore {
USER(“user”);
private String key;
private SharedPreferences prefs = //get this from your activity class
ConfigurationStore(String key){
this.key = key;
}
public String get(){
return prefs.getString(key, null);
}
public void set(String value){
prefs.edit().putString(key, value).commit();
}
}
The usage of the wrapper is shown below
//Set a value:
ConfigurationStore.USER.set("Teddy");
//get a value
ConfigurationStore.USER.get()
It's easy to extend with new properties just by adding to the enum:
public enum ConfigurationStore {
USER(“user”),
DEPLOYMENT_TYPE(“deployment_type”);
....
//Set a value:
ConfigurationStore.DEPLOYMENT_TYPE.set("Beta-test");
//get a value
ConfigurationStore.DEPLOYMENT_TYPE.get()
The question:
The enum is strictly handing String properties.
Is there a way I can make this enum handle different types safely without adding other method signatures (getStringValue, getIntValue)?
I want to be able to do something like:
int age = 23;
String name = "Teddy"
ConfigurattionStore.AGE.set(age)
ConfigurattionStore.NAME.set(name)
...
age = ConfigurattionStore.AGE.get();
name = ConfigurattionStore.NAME.get();

No, not with this design.
To be able to do what you want, you would need to define a generic interface or class
public PrefHandler<T> {
T get();
void set(T);
}
And have multiple instances of this interface:
public class ConfigurationStore {
public static final PrefHandler<String> FOO = ...;
public static final PrefHandler<Integer> BAR = ...;
}

Related

Want to use Enum file name dynamically to fetch constants

I have a config file with key value pair as
language = "IN"
and i have multiple page object enum files with name as
PageObject_US,PageObject_UK,PageObject_IN
every page object enum file has constants that can be accessed using for example
PageObjects_US.String.lable
but what i want to achieve is a way to create something like below
take the parameter from config file store it in a string
like String language = "IN"
Then concatenate using "PageObjects_" + language to get (PageObjects_IN)
so that the returned value can be used to fetch the constants from PageObjects_IN.String.label.
following is the code block:
if(!ENV.equalsIgnoreCase("development") && VALIDATION.equalsIgnoreCase("yes")) {
Elements.ByTitle(webDriver,PageObjects_IN.GREAT.label);
Elements.ByID(webDriver, PageObjects_IN.COUNTER.label);
}
In the above i want to use enum file PageObjects_IN at run time as i have many enum files
below is the enum
public enum PageObjects_IN {
// Text
GREAT("great"),
COUNTER("counter");
public final String lable;
PageObjects_IN(final String lable) {
this.lable = lable;
}
}
This is possible (using reflection) but strongly not recommended as it eliminates the efficiency of Java language constructs.
Not recommended way
Say you have a package click.webelement.cucumber.po where you store
public enum PO_EN {
GREAT("great_en"),
COUNTER("counter_en");
public final String label;
PO_EN(String label){
this.label = label;
}
}
and
public enum PO_IN {
GREAT("great_in"),
COUNTER("counter_in");
public final String label;
PO_IN(String label){
this.label = label;
}
}
Then to take a value you can do something like this:
String lang = "EN";
// Take class
Class clazz = Class.forName("click.webelement.cucumber.po.PO_" + lang);
// Find an object that represent enum constant
Object val = Arrays
.stream(clazz.getEnumConstants()).filter(o -> "GREAT".equals(o.toString()))
.findAny()
.get();
// Take field value for that object
Field f = clazz.getField("label");
System.out.println(f.get(val));
This is error-prone approach and you would not have benefit from compile phase.
Recommended approach - 1
Instead of having enum use classes.
public abstract class PO {
public abstract String great();
public abstract String counter();
}
and
public class PO_EN extends PO{
#Override
public String great() {
return "great_en";
}
#Override
public String counter() {
return "counter_en";
}
}
and
public class PO_IN extends PO{
#Override
public String great() {
return "great_in";
}
#Override
public String counter() {
return "counter_in";
}
}
so your test would be much simpler
String lang = "EN";
Class clazz = Class.forName("click.webelement.cucumber.po.PO_" + lang);
PO val = (PO) clazz.getDeclaredConstructor().newInstance();
System.out.println(val.great());
Recommended approach - 2
You can utilize PageFactory harness for your page objects and use this lib to parametrize your locators, like (if you use test ng):
#DataProvider(name = "languages")
Object[][] dataProvider(){
return new Object[][]{
{"en", "great_en", "counter_en"},
{"in", "great_in", "counter_in"}
};
}
#Test(dataProvider = "languages")
public void testPage(String language, String great, String counter){
DefaultParameterProvider
.properties
.set(Map.of("p.great", great, "p.counter", counter));
MyPage myPage = new MyPage(driver);
...
}
Where your page would be like this:
public class MyPage extends PageObjectParameterized {
#FindByParameterized(xpath = "//button[#name='{wec:p.great}']")
WebElement great;
#FindByParameterized(xpath = "//label[text()='{wec:p.counter}']")
WebElement counter;
#FindBy(xpath = "//input")
WebElement input;
public MyPage(SearchContext searchContext) {
super(searchContext);
}
}

Using specific value of integer variable from TextEdit response in MainActivity, to use in a different class?

I'm attempting to code an app that takes the users numerical specifications from the main activity in the form of a TextEdit input, convert that to an integer and then use that specific value of the integer and use that value in a separate class file which I will use the result of the class in the main activity.
Is this possible? Here's what I've attempted in the global variables of the main activity:
deadzoneValue = findViewById(R.id.TextView_deadzoneInfo);
public EditText threshold, deadzone;
public String deadzoneString = deadzone.getText().toString(); //deadzone being the name of the
public int timeLimit = Integer.parseInt(deadzoneString);
public String thresholdString = threshold.getText().toString();
public static int thresholdLimit = Integer.parseInt(thresholdString);
I'm not sure how to use these in the Deadzone class, which I'm trying to take the specific value and use there.
EDIT: Deadzone.java isn't an activity but a class with functions that are called in the MainActivity.
Using of ClassicSingleton:
public class ClassicSingleton2 {
private static String instance = null;
protected ClassicSingleton2() {
}
public static String getInstance() {
return instance;
}
public static void setInstance(String instance) {
ClassicSingleton2.instance = instance;
}
}
You could change type of instance variable to int ...
And in target code you could get this data:
xRef = ClassicSingleton2.getInstance();
This is very simple.
2:
And using of put (putExtra)
Intent oI = new Intent((FirstActivity)this,SecondActivity.class);
oI.putExtra("XRefCaller",123);
And in target code(activity) :
Bundle oBundle = getIntent().getExtras();
if(oBundle != null){
oXRefCaller = oBundle.getString("XRefCaller",-1);
//checking with -1 if the parameter does not exist or is null
}

Android - Set variable once and use everywhere in app

Basically my question is I want to set lets say a string to A or B that is chosen by the user on the first screen. And then have this string variable be saved to be used on other actives else where in the app. I have see the posts and many like in Android global variable.
But the variable needs to be set and the gotten on each activity the variable isn't saved, once and then can be used everywhere?
How could this be done?
I hope i have explained this well enough, as my question differs from the one above.
The variable is not final it can be changed on the first activity but then I want to use it on the following activities, with out having to pass it with intent to each one.
Thanks for the help in advance.
You can use a public class with static String variable or passing the variable to another Activity with putExtra method.
Example :
public class Global {
public static final String DEVELOPER_KEY;
public static final String PLAYLIST_ID;
}
I have made one class for same purpose :
public class Constant {
public static int NUMBER_OF_TILES;
public static String setTilesId[] = {};
public static String TilesDesc[] = {};
public static String Image[] = {};
public static String TilesName;
public static int numberofbox = 0;
public static boolean isLogedIn = false;
public static String TilesSize = null;
public static boolean from_activity = true;
public static String URL_FOR_PRODUCT_ACTIVITY;
}
Now i can use this variables from anywhere in my APP.
You can put your variable in SharedPreferences. As it exactly matches you requirement.
You can put your variable in first screen and use it anywhere in your application. Also it is not final, you can change whenever you want it will just overwrite existing value of variable.
And MOST important thing is it will be preserved even after application is closed.
So whenever user starts your application next time you can use that variable.
A.java code:
package p2;
public class A{
public static String abc;
}
After declaring the static string in class A use it anywhere in the package using the class name as it is static so value will remain whatever you update it to.
B.java code :
package p1;
public class B{
public void methodTest(){
String s = A.abc;
}
}
Make your string public static
Ex:public static String myvar="hey"//This will be available across all classes
Instead of using static variable You can simply use Shared Preferences because after some time when your application is in background your static value become free automatically by garbage collector and become null
public void setString(Context context, String value) {
SharedPreferences prefs = context.getSharedPreferences("setString",
0);
SharedPreferences.Editor editor = prefs.edit();
editor.putString("getString", value);
editor.commit();
}
public String getString(Context context) {
SharedPreferences prefs = context.getSharedPreferences("setString",
0);
String value = prefs.getString("getString", null);
return value;
}
You can also usw SharedPreferences:
SharedPreferences sets =getSharedPreferences(Choose_a_name, 0);
SharedPreferences.Editor editor=sets.edit();
editor.put string("from user", your string);
editor.commit();
And in your other activities you can retrieve, change and save them again. In that way, you can provide the customized data from the user over the activity lifecycle. Even If your task is completely killed, your data is available on the next launch.
To retrieve the data from another activity:
SharedPreferences sets = getSharedPreferences(yourPrefName,0);
String your string = sets.get string("from user", default value);
I want to give you alternative ideas comparing to intents or static classes. Hope it helps :)
class SharedPref
{
public void setString(Context context, String value) {
SharedPreferences prefs = context.getSharedPreferences("setString",
0);
SharedPreferences.Editor editor = prefs.edit();
editor.putString("getString", value);
editor.commit();
}
public String getString(Context context) {
SharedPreferences prefs = context.getSharedPreferences("setString",
0);
String value = prefs.getString("getString", null);
return value;
}
}
and use this class like
SharePref pref=new SharedPref();
pref.setString("Hellooo");
String value=pref.getString();

Getting value from one activity to another class

can anyone guide me to the right direction, as the situation is
have two classes calssA with Activity while classB is simple java class
1.classA has a editbox where the user inputs some value.
2.classB has a method that computes the user input.
need to get the user input value from classA to classB.
a).cannot get it through passing intent as the other class does not have activity.
b).have tried getter setter method by creating a class thus,calling setter where the user inputs the detail.
and calling the getter in another class.
Still the value is null, so what apparently is missing here.Any guidance to a example or brief explanation would be great.Thanks
You can use your own customized class to store the data and use it any where you need.
DataClass
public class Data {
private String a,b;
private static Data data= new Data( );
/* A private Constructor prevents any other
* class from instantiating.
*/
private Data (){ }
/* Static 'instance' method */
public static Data getInstance( ) {
return data;
}
public String getA()
{
return this.a;
}
public String getB()
{
return this.b;
}
public void setA(String a)
{
this.a = a;
}
public String getB(String b)
{
this.b = b;
}
}
In Activity
Data data = Data.getInstance()
data.setA("Stack");
data.setA("Overflow");
you can use this values in Java file like this
Data data = Data.getInstance()
System.out.println(data.getA());
System.out.println(data.getB());
according to my knowledge here you have to use singleton class.
public class DataHolderClass {
private static DataHolderClass dataObject = null;
private DataHolderClass() {
// left blank intentionally
}
public static DataHolderClass getInstance() {
if (dataObject == null)
dataObject = new DataHolderClass();
return dataObject;
}
private String _ProductNames;
public String get_ProductNames() {
return _ProductNames;
}
public void set_ProductNames(String _ProductNames) {
this._ProductNames = _ProductNames;
}
}
to set data
DataHolderClass.DataHolderClass.set_ProductNames(your data variable);
to get data
DataHolderClass.DataHolderClass.get_ProductNames(your data variable);
You can save the edittext value in SharedPreferences, thus making it available in all activity.
By doing this you can fetch the value in other activity without any hassle.
eg:
SharedPreferences.Editor editor = preferences.edit();
editor.putString("edtTextValue", valueOfEditText);
editor.commit();
Further fetching the value in other activity like this,
preferences.getString("edtTextValue", "");
In your activity class create
public static String valueEntered;
valueEntered=mEdtTxt.getText().toString();
and in your java class where you want
String enteredValue=ClassA.valueEntered;

java - an enum question

I have encountered a weird problem in my app (java).
I have an enum. Something like that
public enum myEnum implement myIntrface{
valueA(1),valueb(2),valuec(3),valued(4)
private int i;
// and then - a constructor
public MyEnum(int number){
i = number;
}
private MyObj obj = new MyObj;
// getter and setter for obj
}
and in another class I have this
MyEnum.valueA.setObj(new Obj(...))
in briefe - I have an enum with a private instance member that has a set and a get.
So far so good -
The only thing that amazes me is that later on I look at the value of the MyEnum.valueA().obj is null.
there is nothing that updates the value to null, I have even gave it a default value in the constructor and I still see it null later.
any suggestions?
Enums should be un-modifiable classes so you shouldn't really be doing this. If your looking to modify the state of a type based object like an enum you should use an final class approach with embedded constants. Below is an example of a class based approach with a modifiable name an a un-modifiable name...
public final class Connection {
public static final Connection EMAIL = new Connection("email");
public static final Connection PHONE = new Connection("phone");
public static final Connection FAX = new Connection("fax");
/**/
private final String unmodifiableName; //<-- it's final
private String modifiableName;
/*
* The constructor is private so no new connections can be created outside.
*/
private Connection(String name) {
this.unmodifiableName = name;
}
public String getUnmodifiableName() {
return unmodifiableName;
}
public String getModifiableName() {
return modifiableName;
}
public void setModifiableName(String modifiableName) {
this.modifiableName = modifiableName;
}
}
The purpose of enums is to represent constant values. It does not make any sense to set the fields of a constant value.
You should declare your fields as final, and use the constructor to initialize all of them.
For reference, the following code works as expected:
public class Test {
public static enum MyEnum {
valueA(1),valueb(2),valuec(3),valued(4);
private int i;
private Object o;
private MyEnum(int number) {
i = number;
}
public void set(Object o) {
this.o = o;
}
public Object get() {
return o;
}
}
public static void main(String[] args) {
System.out.println(MyEnum.valueA.get()); // prints "null"
MyEnum.valueA.set(new Integer(42));
System.out.println(MyEnum.valueA.get()); // prints "42"
}
}
the cause of this problem is the db40 framework . It loads an enum from the db using reflection. This is well documented .
http://developer.db4o.com/Forums/tabid/98/aft/5439/Default.aspx

Categories