zcwease / guava-libraries

Automatically exported from code.google.com/p/guava-libraries
Apache License 2.0
0 stars 0 forks source link

Lists.charactersOf(CharSequence), returning List<Character> #15

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
This is easy to implement (though requires some tricks that are not totally
obvious in order to get it just right) and I'm pretty sure it would come in
handy from time to time.

Original issue reported on code.google.com by kevin...@gmail.com on 23 Oct 2007 at 4:31

GoogleCodeExporter commented 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

GoogleCodeExporter commented 9 years ago
Like... List.charactersOf(List<String>, charSequence) ?

Original comment by gonzaloa...@gmail.com on 19 Nov 2007 at 3:39

GoogleCodeExporter commented 9 years ago
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

GoogleCodeExporter commented 9 years ago
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

GoogleCodeExporter commented 9 years ago
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

GoogleCodeExporter commented 9 years ago
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:

GoogleCodeExporter commented 9 years ago
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

GoogleCodeExporter commented 9 years ago
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

GoogleCodeExporter commented 9 years ago
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

GoogleCodeExporter commented 9 years ago

Original comment by kevin...@gmail.com on 17 Sep 2009 at 6:02

GoogleCodeExporter commented 9 years ago
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

GoogleCodeExporter commented 9 years ago

Original comment by kevinb@google.com on 30 Jul 2010 at 3:53

GoogleCodeExporter commented 9 years ago

Original comment by kevinb@google.com on 30 Jul 2010 at 3:56

GoogleCodeExporter commented 9 years ago

Original comment by kevinb@google.com on 14 Sep 2010 at 8:49

GoogleCodeExporter commented 9 years ago
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

GoogleCodeExporter commented 9 years ago

Original comment by cgdecker@google.com on 3 Nov 2014 at 9:10