Closed GoogleCodeExporter closed 9 years ago
You want to get a list of characters specified in the CharSequence of a certain
List
right?.
Original comment by gonzaloa...@gmail.com
on 19 Nov 2007 at 3:37
Like... List.charactersOf(List<String>, charSequence) ?
Original comment by gonzaloa...@gmail.com
on 19 Nov 2007 at 3:39
No, just view a CharSequence as a List, for example Lists.charactersof("abc")
returns
['a', 'b', 'c'].
Original comment by kevin...@gmail.com
on 19 Nov 2007 at 6:06
You can use something like that (sorry for my english) =), im just trying to
help.
Regards!
/**
* Returns an {@code List} instance given the charSequence.
*
* @param charSequence the elements that the list should contain, in order.
* @return an {@code List} instance containing those elements.
* */
public static List<CharSequence> charactersOf(CharSequence charSequence) {
checkNotNull(charSequence);
List<CharSequence> charsList = newArrayList();
for (int i = 0; i < charSequence.length(); i++) {
charsList.add(charSequence.subSequence(i, i+1));
}
return charsList;
}
Original comment by gonzaloa...@gmail.com
on 19 Nov 2007 at 7:15
Also, we could add this validation to avoid the creation of the new list.
if (charSequence.length() == 0) {
return null; //Or whatever you want... (emptyList?)
}
Regards!
Original comment by gonzaloa...@gmail.com
on 20 Nov 2007 at 2:20
I implemented this as a view of the character sequence in the attached patch,
I'd
really appreciate any review and comment on it even if its not included as this
is
one of my very first open source contributions.
Original comment by mohammad...@gmail.com
on 4 May 2009 at 7:57
Attachments:
I took a quick look over charctersOf.txt, and have a few questions/suggestions.
I
don't have the experience to comment on the overall structure/approach, but
hopefully
these comments are helpful:
* There is no guarantee that a given CharSequence is immutable, and it looks like
subSequence(int, int) returns a copy, rather than a view, so the implementation
of
CharSequenceListView.subList(int,int) may not maintain the contract specified
in the
List api for subList(int,int). (This can be seen by using subSequence and
delete on
a StringBuffer.)
* Is it necessary to redefine add/addAll/remove/set given that
CharSequenceListView extends ImmutableCollection<Character>?
* Should the overridden methods all have @Overrides annotations? (get/add/etc.)
* I believe all the fields could be final.
* @NonNull annotation on parameter of charactersOf(CharSequence)?
* javadoc on charactersOf(CharSequence)
* Would it make sense to have the ListIterator extend
UnmodifiableIterator? (reusing the remove() method defined
therin). It seems like what you want is an
UnmodifiableListIterator, which doesn't seem to exist yet.
Original comment by cresw...@gmail.com
on 4 May 2009 at 5:47
thanks for taking time to read the code and comment :-), ok here goes:
1-you are absolutely right, i will rewrite the sublist function to view the
same list
probably with start and end fields limiting the operations and altering the
behavior
of the iterators.
2-yes in case i am extending ImmutableCollection, I should've extended
ImmutableList
though.
3-you are absolutely right I should do that.
4-you are right.
5-sure I just wrote the code as a draft to get feedback about the approach.
6-ya I looked for an UnmodifiableListIterator but couldn't find any and found
myself
asking the same question and decided to do it the direct way and just change it
if
there was a good reason.
I also figured out a better way to do the iterators (make them fail by saving
the
initial size and checking the current size each time instead of iterate on a
copy,
thanks for pointing out that subsequence returns a copy not a view, this won't
detect
any set kind of operations though -- any ideas ?).
Original comment by mohammad...@gmail.com
on 5 May 2009 at 6:36
if i actually viewed the list with start and end fields how would i know if the
sequence has changed and that these fields should be updated?
Original comment by mohammad...@gmail.com
on 5 May 2009 at 11:18
Original comment by kevin...@gmail.com
on 17 Sep 2009 at 6:02
Wouldn't com.google.common.primitives.Chars be a better place for this than
com.google.common.collect.Lists, e.g. an overload of Chars.asList()?
Original comment by fin...@gmail.com
on 1 Feb 2010 at 3:03
Original comment by kevinb@google.com
on 30 Jul 2010 at 3:53
Original comment by kevinb@google.com
on 30 Jul 2010 at 3:56
Original comment by kevinb@google.com
on 14 Sep 2010 at 8:49
This issue has been migrated to GitHub.
It can be found at https://github.com/google/guava/issues/<id>
Original comment by cgdecker@google.com
on 1 Nov 2014 at 4:16
Original comment by cgdecker@google.com
on 3 Nov 2014 at 9:10
Original issue reported on code.google.com by
kevin...@gmail.com
on 23 Oct 2007 at 4:31