/ Published in: Java
A custom class for retrieving deliminated data from a (custom) MyString string.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
public class DataExtractor { // Data Objects ( private MyString data; private MyString delim; // The specified deliminator private int[] delims; // An array that will hold the positions of deliminators within a string of data. // ) // Constructors ( public DataExtractor() { // Default Constructor this(new MyString(), new MyString(" "), new int[]{0}); } this(new MyString(s), new MyString(" "), new int[]{0}); } this(s, d, new int[]{0}); } this(new MyString(s), new MyString(d), dlms); } public DataExtractor(MyString s) { // Parameterized Constructor this(s, new MyString(" "), new int[]{0}); } public DataExtractor(MyString s, MyString d) { // Parameterized Constructor this(s, d, new int[]{0}); } public DataExtractor(MyString s, MyString d, int[] dlms) { // Parameterized Constructor Template data = new MyString(s); delim = new MyString(d); delims = dlms; } public DataExtractor(DataExtractor d) { // Copy Constructor this(d.getData(), d.getDelim(), d.getDelims()); } // ) // Accessors ( private MyString getData() { return new MyString(data); } private MyString getDelim() { return new MyString(delim); } private int[] getDelims() { return delims; } // ) // Mutators ( public void setBuffer(MyString d) { delim = new MyString(d); try { delims = data.findAll(delim.toString()); // The findAll method of my MyString class ended up coming very much in handy!! } delims = new int[]{0}; } } this.setBuffer(new MyString(d)); } public void setBuffer() { this.setBuffer(new MyString(" ")); } public MyString get(int n) { // This assumes the client will know of what type the desired piece of data is to convert it from a String later. char[] itemChars = null; if ( n==0 ) { itemChars = new char[delims[n]]; for (int i=0; i<delims[n]; i++) { itemChars[i] = data.get(i); } } if ( n>0 && n<delims.length ) { itemChars = new char[(delims[n] - delims[n-1]) - 1]; // The size of the piece of data is (delims[n+1]-delims[n])-1 for (int i=delims[n-1]+1; i<delims[n]; i++) { // First character of the nth piece of data is at data.get(delims[n-1]+1) and last character is at data.get(delims[n]-1) itemChars[i-(delims[n-1]+1)] = data.get(i); } } if ( n == delims.length ) { itemChars = new char[(data.length() - delims[n-1]) - 1]; for (int i=delims[n-1]+1; i<data.length(); i++) { itemChars[i-(delims[n-1]+1)] = data.get(i); } } return new MyString(itemChars); } // ) }