001 package railo.commons.lang; 002 003 import java.util.ArrayList; 004 005 /** 006 * a Simple single direction string list 007 */ 008 public final class StringList { 009 010 011 012 private final Entry root=new Entry(null,Entry.NUL); 013 private Entry curr; 014 private int count=0; 015 016 /** 017 * constructor of the class 018 */ 019 public StringList() { 020 curr=root; 021 } 022 023 /** 024 * constructor of the class 025 * @param str String Element 026 */ 027 public StringList(String str) { 028 root.next=new Entry(str,Entry.NUL); 029 curr=root.next; 030 count=1; 031 } 032 033 /** 034 * constructor of the class, initalize with 2 values 035 * @param str1 036 * @param str2 037 */ 038 public StringList(String str1, String str2) { 039 this(str1); 040 add(str2); 041 } 042 043 /** 044 * @return returns if List has a next Element 045 */ 046 public boolean hasNext() { 047 return curr.next!=null; 048 } 049 050 /** 051 * @return returns if List has a next Element 052 */ 053 public boolean hasNextNext() { 054 return curr.next!=null && curr.next.next!=null; 055 } 056 057 /** 058 * @return returns next element in the list 059 */ 060 public String next() { 061 curr=curr.next; 062 return curr.data; 063 } 064 public char delimeter() { 065 return curr.delimeter; 066 } 067 068 /** 069 * @return returns current element in the list 070 */ 071 public String current() { 072 return curr.data; 073 } 074 075 /** 076 * reset the String List 077 * @return 078 */ 079 public StringList reset() { 080 curr=root; 081 return this; 082 } 083 084 /** 085 * @return returns the size of the list 086 */ 087 public int size() { 088 return count; 089 } 090 091 092 /** 093 * adds a element to the list 094 * @param str String Element to add 095 */ 096 public void add(String str) { 097 curr.next=new Entry(str,Entry.NUL); 098 curr=curr.next; 099 count++; 100 } 101 public void add(String str, char delimeter) { 102 curr.next=new Entry(str,delimeter); 103 curr=curr.next; 104 count++; 105 } 106 107 private class Entry { 108 private static final char NUL=(char)0; 109 private Entry next; 110 private String data; 111 private char delimeter; 112 private Entry(String data, char delimeter) { 113 this.data=data; 114 this.delimeter=delimeter; 115 } 116 } 117 118 public String[] toArray() { 119 ArrayList<String> list=new ArrayList<String>(); 120 while(hasNext()){ 121 list.add(next()); 122 } 123 return list.toArray(new String[list.size()]); 124 } 125 }