implement Bookmark list in java - java

I am developing a program which has three JTextBox which my users can enter and check some text for right rule.
So I want add a ablitiy to my program that my users can add or remove their favorite text to a Favorite List and can create folder in Favorite list and put some text in it, such as Bookmark library in FireFox or other web browser.
I want use RandomAccessFile to save favorite list as a favorite source.
How do I implemet it? is there beter way to implement it? is there beter way from RandomAccessFile?
Can any one help me?
Thanks.

There could be lots of approaches. It all depends on what you want to achieve.
Consider using Java serialization mechanism. You can serialize a collection of bookmarks to a file. When your app starts, you deserialize it, and get the same collection data.
The advantages are: simple and easy implementation. The disadvantages: you can't look through stored bookmarks in a text editor or something. The same class hierarchy is to be used to load the serialized version.
XML is human-readable and provides easy interoperability. Other applications would be able to handle your list of bookmarks.
It usually takes more resources to parse the XML and load it to memory and then to create the internal object structures. Though you can use the DOM to traverse the tree all the time, it could be not as convenient as the internal data structure using specialized classes.
Random Access Files work best with fixed record sizes. It means all the fields of your bookmarks must be fixed-length. For example, the name of a bookmark is String. When you write it out to a file, you store it like an array of a fixed length, let's say 20. This automatically implies that if users give a bookmark the name which length is greater than 20, the remaining characters would be lost.
It is also easy to implement with the caveats above. Of course the records could be of variable length, but then you lose the random access to file because you cannot easily calculate the position of a specific record.
Firefox uses JSON for storing bookmarks and allows exporting to HTML. You can explore this too.

You can also store bookmarks, and things you want to keep between sessions in the Preferences,
see http://download.oracle.com/javase/6/docs/api/java/util/prefs/Preferences.html

Related

What is the best way to store data in a Page Object Model Framework

Im building an automation framework in selenium using the Page Object Design Pattern.
Following are some of the data that Im using and where i have stored them
PageObjects (xpath, id etc) - In the Page Classes itself
Configuration Data (wait-times, browser type , the URL etc) - In a properties file.
Other data - In a class as static variables.
Once the framework starts growing it would be hard to store all the data it would be hard to organize the data. I did a some research on how others have implemented the way they store data in their framework. Here is what I found out,
Storing data (mostly page objects) in classes itself
Storing data in JSON
And some even suggested storing data in a database so that it would reduce reading times
Since there are lot of options out there, I thought of getting some feedback on what is the best way to store data and how everyone else has stored there data.
JSON or Any temp data storage is the best option as it is a framework and the purpose of it is to reuse for different projects.
I don't see any problem with the way you have stored your data.
Locators (by POM definition) should be stored in the page objects themselves.
Config data can be stored in some sort of config file... whatever you find convenient. You can use plain text, JSON, XML, etc. We use XML but that really comes down to personal preference.
I think this is fine also.
The framework doesn't really grow, the automation suite does. As long as you keep the data stored in the 3 places above consistently, I think you should be fine. The only issue I've run into with this approach is that sometimes certain pages have a LOT of functionality on them so the page objects grow quite large. In those cases, we found a way to divide the page into smaller chunks, e.g. one page had 22 tabs, each consisting of a different panel. In that case, we broke the page object into 22 different class files to keep the size more manageable and then hooked them all back into the main page as properties, e.g. mainPage.Panel1.someMethodOnPanel1();
I advice using Interfaces for each device type to store multiple type selectors, example:
import static org.openqa.selenium.By.cssSelector;
import static org.openqa.selenium.By.linkText;
import static org.openqa.selenium.By.xpath;
public interface DesktopMainPageSelector {
By FIRST_ELEMENT = cssSelector("selector_here");
By SECOND_ELEMENT = xpath("selector_here");
By THIRD_ELEMENT = id("selector_here");
}
than, just implement these selectors from whatever you need them.
You can also use enums with for a more complex structure.
I found this as best solution, because its easy to manage large numbers of selectors

Store multiple values in a file - best format?

I want to store multiple values (String, Int and Date) in a file via Java in Android Studio.
I don't have that much experience in that area, so I tried to google a bit, but I didn't get the solution, which I've been looking for. So, maybe you can recommend me something?
What I've tried so far:
Android offers a SharedPreferences feature, which allows a user to save a primitive value for a key. But I have multiple values for a key, so that won't work for me.
Another option is saving data on an external storage medium as file. As far as good. But I want to keep the filesize at minimum and load the file as fast as possible. That's the place, where I can't get ahead. If I directly save all values as simple text, I would need to parse the .txt file per hand to load the data which will take time for multiple entries.
Is there a possibility to save multiple entries with multiple values for a particular key in an efficient way?
No need to reinvent a bicycle. Most probably the best option for your case is using the databases. Look into Sqlite or Realm.
You don’t divulge enough details about your data structure or volume, so it is difficult to give a specific solution.
Generally speaking, you have these three choices.
Serialize a collection
I have multiple values for a key
You could use a Map with a List or Set as its value. This has been discussed countless times on Stack Overflow.
Then use Serialization to write and read to storage.
Text file
Write a text file.
Use Tab-delimited or CSV format if appropriate. I suggest using the Apache Commons CSV library for that.
Database
If you have much data, or concurrency issues with multiple threads, use a database such as the H2 Database Engine.

Android Intents and Lists

I plan on reading several files when my app/game is created and using the information from them for the entirety of the app. I also have to write to the file at one point.
I have two files. One is a 2-column text file that I'll turn into a dictionary for fast searching. The other is a text file that has 11 columns. I'll make a dictionary out of two of the columns, and the other data I need kept as is so I can write to the columns to count the amount of times something happens in different circumstances for datamining.
Currently, I've turned the second file into a list of a list of strings, or List>. I can't figure out how to pass that around in intents. ".putStringArrayListExtra" only works for a list of strings.
Am I going about this the wrong way entirely? This is my first real Android app.
In order to store a data structure into an Intent, it has to be either serializable or parcelable. If your data structure is neither of them, you might create a class that would implement Serializable and manage it. A good example might be found here.
Once done, you then might use Intent.putSerializable(...) to store your data structure. See this:
Using putSerializable in Android
Additionally to this, if you could convert your structure into a JSON structure, you'd already have it done since it would be treated as a String. If not, the above solution should be easy to do.

Recalling information form an array in list form

I am trying to recall a list of employees from an array in Java. I can either choose to do them individually via their ID number or bring all employee info up at once. I know how to code everything else but I cannot find a clear tutorial on how to recall the data and project it onto the text box I have programmed into my GUI. I have the array already created in a different class but I need to get the information from that one and display it in the GUI. Any help would be appreciated. Thanks.
If you want more info just let me know.
[The image is a screenshot of the simple GUI I made.]
There are many ways to do this, but one simple way that may help you to understand this process is as follows:
Implement a store method that simply writes all data to a file in a format of your choice. XML is common, as is TSV or CSV. Some of those are very easy to use, as predefined classes exist to handle them partially or completely.
You will have to read the data you want to store from whereever you have it in your application, possibly in the text boxes directly.
Accordingly implement a restore method that reads the file and fills it back into your data structure and/or the text boxes.

Java saving strings

I have a RuneScape Private Server project coded in Java, and am trying to code a personal "tag" that players can use. I have managed to do this, but everytime there is a restart on the server, their "tag" gets reset to "null".
Their "tag" is initalized by doing a command ";;settag [name]". Their tag is then set to whatever they want. I have done this through a string:
if (command[0].equals("settag")) {
newTag = getCompleteString(command, 1);
newTag = player.yellTag
player.sendMessage("Your tag is now:" +newTag);
}
I am unsure what the most efficient way to fix this would be, I am thinking of just loading and saving through .xml/.txt files. By the way, player.yellTag is where the next command (::mytag) searches it from, which works fine, until there is a restart of the server.
it all depends on the context of your application. If you are planning on having less than a few hundreds players, then a xml file may be ok. You should look at JAXB, which is, afaict, the standard way to store your objects in Java. You can also store them as JSON files, using gson which is way simpler to use and implement than XML stuff.
But if you get to have more than thousands of players, you may want to get some more efficient way to serialize your tags by putting them in a database, and thus an ORM library like hibernate could help you do that.
You may want to make your own stuff, like a tag directory full of files named after unique ids of your players containing the players' tag... It's a lot more "hackish" but still quite efficient.

Categories

Resources