Store method with its parameter values? - java

I've a java function like
String abc="myvalue";
myfunction(String abc);
Is there any possibility to store this myfucntion("myvalue"); in an object to recall it automatically?
Explanation:
I've a function with many parameters values may 20-30, my application is being auto-starting when windows is login.
Once user launched the function first time I need to store that function with is passed values for each parameter. So that when an application will be auto-start it can recall that function from its storing place.

Theoretically you could do that using reflection. But that would be well, madness.
You do not store method invocations.
You store data.
You carefully decide who to slice information into reasonable "records", and then you store that data using a decent format, for example JSON or XML. You define a configuration object for example, and then you store the information in that object. And later on, that information is loaded from disk, and then you apply that.

Related

What is the best possible structure for a db to store configurations date

I have a requirement where i need to store different application config data , so just wanted to know the best possible way to to store it.
Currently I am using below fields for my db
id, name ,category ,city,value(text field), int_value, float_value, string_value,date_value,bool_value
value field will be used to store complex data which in turn are json object capable of storing different key value pair.
for eg.
value:
{ "is_enabled" : true,
"list_of_applicable_ids" : ["123","345","567","890"]
}
And the reason I have added different value data type field(int_value, float_value) because it will be easy to query on that fields and index will make this even faster.
So just wanted to know the better approach to store these kind of data in db.
will using only value fields enough for my requirements ?
Frequency of config changes are very less( once or twice in a month)
Take a look a file based databases.
An example would be MongoDB.
Mongo uses a data form that closely resembles JSON (called BSON), and could in your case store the entire config file without the need to predefined it's fields.

How to check which of the Object parameters got changed

I have a situation where i am changing few parameters values of an Object.
UserDetails has around 14 parameters.I am changing the values of few parameters and submitting them from a Form .These values should get updated on the database back-end.
Are there any inbuilt functions to check if any of the values got changed?
Are there any inbuilt functions to say which of the values got changed?
No.
Are there any inbuilt functions to check if any of the values got changed?
No.
However, you can implement your own methods to test these things. An equals method is easy to implement, and indeed many IDEs have "wizards" to generate them. A "what has changed" method is more complicated. The complexity comes in how the method tells the caller what fields have changed, and how the caller can make use of this information.
Alternatively, Apache Commons provides a class called EqualsBuilder that uses reflection, etcetera to compare objects based on their fields.
I also agree with JB Nizet. If you are doing this in an attempt to optimize database updates, you are probably wasting your time. You are probably better off just saving the all of the fields.
Consider this. Unless your front-end caches the old values of the fields read from the database while the user is updating the form (or not), your front end is going to have to re-query the database to find the old value. You would be better off just issuing the UPDATE to update all of the fields than doing a SELECT followed by a conditional UPDATE is something has changed.
Probably you can check this link.. I am not sure this can be done in java. But, you can try with javascript. Please check this link. You can do with EXT.js
handler: function(btn, evt) {
var f = btn.up('form').getForm();
f.submit({
url: '/some-path-on-my-server/save/,
getParams: function(useModelValues) {
var falseVal = false;
var fieldParams = this.form.getValues(falseVal, true, this.submitEmptyText !== falseVal, useModelValues, true);
return Ext.apply({}, fieldParams);
}
});
}
https://www.sencha.com/forum/showthread.php?173867-I-want-to-submit-only-dirty-field-values.

Passing arguments to java function in bulk

What would you use if you wanted to pass a list of options into a function?
For example, if you have an interface to a server:
public interface Server {
public void authUser(String username, String password, <xyz> options);
}
What structure would use use for to pass a set of options? Something like a HashMap?
The reason I'm saying that it comes from tunnel vision is because I feel that this goes against Java standards. Java has method overloading. So if I get flames for raising the question I understand. But overall, maybe in different cases, would you ever pass bulk data in some collection and, if yes, which one?
Option1 : If you are choosing any collections like List or Set these are specific to an object . I mean,
Lets Assume, Set sets = new HashSet();
If I want 5 Object of different different class having no relationship to be send, then It would be very difficult to recognize that which Object is belong to which class while Iteration. So, I wont recommend Collections.
Option2 : If you are choosing Map, the same above problem may occurs while getting the Object Dynamically. So, This Options is also not recommended.
Option3 :
Why cann't you create your own DTO and in that DTO place your reqyired datastructure and pass it over.
If you want 5 different Object to be pass then, you can pass. If all are of same type then you may use Collection or array or Variable Arguement based on your scenerio.
I think anything Serializable is exactly the thing. If you can serialize the object, then you can pass (store, transmit...) it, passing it's properties in bulk. What format of serialized data to choose, is another question.
It depends on the data you want to pass.
You can use a map(hashmap) if you are passing key-value pairs.
If it is just a list of diffrent object, you can use List(ArrayList)
Other option is to create DTO(data transfer object) with getter and setter methods.
You may want to take a look at VARARGS feature that was introduced in JAVA5.
I'd suggest a Map [HashMap] as you can then access the argument values via their Keys.

String Compression: Passing Object Through URL

I have two pages, say A and B. User navigates from Page-A to Page-B. Page-B needs to have some values which are passed from the Page-A. Among these values some of them are Objects. Now I have the following ways to pass the parameter to Page-B
Store the Objects in some Scope (say Session, Page-Flow).
Pass the Objects as query string after converting them into String.
The drawbacks of the above two ways are followings respectively:
If User bookmarks the Page-B for later usage and try to access it from different session, it generates Exception. As the Objects are not present in the scope.
There are limitations of the length of the URL, which is 2048 Character (ref). So if I convert it to JSON and try to pass it through URL and if the JSON String is more than the limited characters, I would get JSON Exception from Page-B.
Can I compress the String representation of the Object so that it will not exceed the limitation Character?
How can I solve this issue (by any other means)?
Any solution is greatly appreciated.
Putting serialized objects in the URL is a really bad idea. If you want to access state via URL (GET parameters) then normally the URL should only contain some way of identifying the item in question.
Fortunately if you're using a database back end to persist your objects then the database will usually give them an identity for you, in the form of a primary key. You can then just put the ID in the URL and have Java retrieve the object for that ID whenever it receives a request for it.
If you don't use a database back end then it's up to you to give your objects an identity that they can be located by. The simplest solution would be to store references to the objects in a map and put the map key in the URL.

Is it possible to send an object to a file in a format that is human readable?

I am working on a project for my advanced Java class, and the assignment says he wants us
to send an object to a file, which is easy enough, but that he also wants the file to be human readable and editable. I sent him an e-mail 3 days ago and he hasn't responded, so
I am kind of stuck between a rock and hard place since the project is due in 3 days.
So would any of you clever programmers be able to fill me in on the secret that apparently I am left out of.
How do you send an object to a file that reads like English?
I want to have the ability to both read and write a to-do item to a
file. I see our application looking like:
When it first starts, the program asks the user if there is a file containing to-do items. If so, the user will name the file, the
program will read it in and continue.
When the user decides to exit, the program will prompt the user - to ask if the to-do items should be saved to a file - if so, the
user will name the file and the program will write them out in
such a fashion that it can read them in again.
I want these file to be human readable (and editable). No binary data.
No counting. My advice to you would be to have a method somewhere that
looked like:
public ToDoItem getToDoItem(FileInputStream fis) {
// ...
}
and
public void writeToDoItem(FileOutputStream fos) {
// ...
}
Think of your serialization model. The ObjectOutputStream might write bytes, but is there another way you could represent the object and write it through some other output stream that writes human-readable text?
This is going to depend on the type of object you have. You will have to tailor it to a particular type of data.
For example, if you have an Object
String title;
List<Integer> ids;
then you could save it as JSON
{
title: 'aaaa',
ids: [ 1,2,3,4,5 ]
}
which is equivalent, but much more readable than a binary ObjectOutputStream.
Again, this won't work for all kinds of data.
There is an XML-based bean serialization, too, which also works with almost all data, but I would not call that human-readable.
Think how you would represent an object on paper in such a way that it could be reconstructed unambiguously. You'd probably list the class name, then you'd list each field name and its current value. If the field was a primitive, the value would be just the primitive value. If it was a reference type, you'd represent the object recursively using this procedure. If it was an array, you'd list each element value.
There are various standard ways of formatting such a representation (XML and JSON to name a couple). The key is to make it a text-only representation so it is human-readable.
You can have a try with JAXB.(Java Architecture for XML Binding)
It can send a JAXB styled object to a xml file.
But you should define a XML Schema file at first.
For more:http://jaxb.java.net/tutorial/
The human readable that you desire could be XML or JSON. My answer How to create object tree from xsd in Java?
might help in giving you pointers to the approach you can follow to achieve what you want.

Categories