package com.ebuddy.cassandra;

import java.util.Arrays;

/**
 * // TODO: Add class description here.
 *
 * @author Eric Zoerner <a href="mailto:ezoerner@ebuddy.com">ezoerner@ebuddy.com</a>
 */
public class Path implements Comparable<Path> {
    private static final char PATH_DELIMITER_CHAR = '|';
    private static final String LIST_INDEX_PREFIX = "@";

    private final String[] pathElements;

    public Path(String string) {
        this(new String[]{string});
    }

    private Path(String[] pathElements) {
        this.pathElements = pathElements;
    }

    public Path concatenate(Path other) {
        String[] newPathElements = new String[pathElements.length + other.pathElements.length];
        System.arraycopy(pathElements, 0, newPathElements, 0, pathElements.length);
        System.arraycopy(other.pathElements, 0, newPathElements, pathElements.length, other.pathElements.length);
        return new Path(newPathElements);
    }

    /**
     * Return true if the head element in this path is a list index.
     */
    public boolean isListIndex() {
        String head = pathElements[0];
        if (!head.startsWith(LIST_INDEX_PREFIX)) {
            return false;
        }
        String rest = head.substring(LIST_INDEX_PREFIX.length());
        int index;
        try {
            index = Integer.parseInt(rest);
        } catch (NumberFormatException ignored) {
            return false;
        }
        return index >= 0;
    }

    /**
     * Paths always end with the delimiter character in order to facilitate
     * start/finish slice queries in Cassandra.
     * @return the String representation of a path.
     */
    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        for (String pathElement : Arrays.asList(pathElements)) {
            builder.append(pathElement);
            builder.append(PATH_DELIMITER_CHAR);
        }
        return builder.toString();
    }

    public static Path fromIndex(int i) {
        return new Path(LIST_INDEX_PREFIX + i);
    }

    public static Path fromObject(Object o) {
        if (o instanceof Path) {
            return (Path)o;
        }
        return new Path(o.toString());
    }

    @Override
    public int compareTo(Path o) {
        return toString().compareTo(o.toString());
    }
}

@@ -53,51 +53,181 @@
 ys;%0A
-%0A/**%0A * // TODO: Add class description here
+import java.util.LinkedList;%0Aimport java.util.List;%0A%0A/**%0A * A path used as column names in Cassandra for encoding structures and for querying elements of a structured object
 .%0A *
@@ -497,24 +497,28 @@
 e final 
+List%3C
 String
-%5B%5D
+%3E
  pathEle
@@ -575,28 +575,29 @@
 his(
-new String%5B%5D%7B
+Arrays.asList(
 string
-%7D
+)
 );%0A 
@@ -619,24 +619,28 @@
 te Path(
+List%3C
 String
-%5B%5D
+%3E
  pathEle
@@ -751,16 +751,20 @@
     
+List%3C
 String
-%5B%5D
+%3E
  new
@@ -786,106 +786,37 @@
 new 
-String%5BpathElements.length + other.pathElements.length%5D;%0A        System.arraycopy(pathElements, 0,
+LinkedList%3CString%3E();%0A       
  new
@@ -827,21 +827,24 @@
 Elements
-, 0, 
+.addAll(
 pathElem
@@ -839,39 +839,32 @@
 All(pathElements
-.length
 );%0A        Syste
@@ -862,86 +862,31 @@
     
-System.arraycopy(other.pathElements, 0, newPathElements, pathElements.length, 
+newPathElements.addAll(
 othe
@@ -899,23 +899,16 @@
 Elements
-.length
 );%0A     
@@ -1109,11 +1109,15 @@
 ents
-%5B0%5D
+.get(0)
 ;%0A  
@@ -1788,30 +1788,16 @@
 ement : 
-Arrays.asList(
 pathElem
@@ -1801,17 +1801,16 @@
 lements)
-)
  %7B%0A     

use a List of Strings in a Path instead of String[]