001/** 002 * 003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved. 004 * 005 * This library is free software; you can redistribute it and/or 006 * modify it under the terms of the GNU Lesser General Public 007 * License as published by the Free Software Foundation; either 008 * version 2.1 of the License, or (at your option) any later version. 009 * 010 * This library is distributed in the hope that it will be useful, 011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 013 * Lesser General Public License for more details. 014 * 015 * You should have received a copy of the GNU Lesser General Public 016 * License along with this library. If not, see <http://www.gnu.org/licenses/>. 017 * 018 **/ 019package lucee.commons.lang; 020 021import java.util.ArrayList; 022 023/** 024 * a Simple single direction string list 025 */ 026public final class StringList { 027 028 029 030 private final Entry root=new Entry(null,Entry.NUL); 031 private Entry curr; 032 private int count=0; 033 034 /** 035 * constructor of the class 036 */ 037 public StringList() { 038 curr=root; 039 } 040 041 /** 042 * constructor of the class 043 * @param str String Element 044 */ 045 public StringList(String str) { 046 root.next=new Entry(str,Entry.NUL); 047 curr=root.next; 048 count=1; 049 } 050 051 /** 052 * constructor of the class, initalize with 2 values 053 * @param str1 054 * @param str2 055 */ 056 public StringList(String str1, String str2) { 057 this(str1); 058 add(str2); 059 } 060 061 /** 062 * @return returns if List has a next Element 063 */ 064 public boolean hasNext() { 065 return curr.next!=null; 066 } 067 068 /** 069 * @return returns if List has a next Element 070 */ 071 public boolean hasNextNext() { 072 return curr.next!=null && curr.next.next!=null; 073 } 074 075 /** 076 * @return returns next element in the list 077 */ 078 public String next() { 079 curr=curr.next; 080 return curr.data; 081 } 082 public char delimiter() { 083 return curr.delimiter; 084 } 085 086 /** 087 * @return returns current element in the list 088 */ 089 public String current() { 090 return curr.data; 091 } 092 093 /** 094 * reset the String List 095 * @return 096 */ 097 public StringList reset() { 098 curr=root; 099 return this; 100 } 101 102 /** 103 * @return returns the size of the list 104 */ 105 public int size() { 106 return count; 107 } 108 109 110 /** 111 * adds a element to the list 112 * @param str String Element to add 113 */ 114 public void add(String str) { 115 curr.next=new Entry(str,Entry.NUL); 116 curr=curr.next; 117 count++; 118 } 119 public void add(String str, char delimiter) { 120 curr.next=new Entry(str,delimiter); 121 curr=curr.next; 122 count++; 123 } 124 125 private class Entry { 126 private static final char NUL=(char)0; 127 private Entry next; 128 private String data; 129 private char delimiter; 130 private Entry(String data, char delimiter) { 131 this.data=data; 132 this.delimiter=delimiter; 133 } 134 } 135 136 public String[] toArray() { 137 ArrayList<String> list=new ArrayList<String>(); 138 while(hasNext()){ 139 list.add(next()); 140 } 141 return list.toArray(new String[list.size()]); 142 } 143}