// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.config;

import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

/**
 * A vector of leaf nodes.
 *
 * @author gjoranv
 * @since 5.1.4
 */
public class LeafNodeVector<REAL, NODE extends LeafNode<REAL>> extends NodeVector<NODE> {

    NODE defaultNode;

    /**
     * Creates a new vector with the given default node.
     */
    // TODO: remove this ctor when the library uses reflection via builders, and resizing won't be necessary
    public LeafNodeVector(NODE defaultNode) {
        assert (defaultNode != null) : "The default node cannot be null";

        this.defaultNode = defaultNode;
        if (createNew() == null) {
            throw new NullPointerException("Unable to duplicate the default node.");
        }
    }

    // TODO: take class instead of default node when the library uses reflection via builders
    public LeafNodeVector(List<REAL> values, NODE defaultNode) {
        this(defaultNode);
        for (REAL value : values) {
            NODE node = createNew();
            node.value = value;
            vector.add(node);
        }
    }

    /**
     * Creates a new Node by cloning the default node.
     */
    @SuppressWarnings("unchecked")
    private NODE createNew() {
        return (NODE) (defaultNode).clone();
    }

    // TODO: create unmodifiable list in ctor when the library uses reflection via builders
    @SuppressWarnings("unchecked")
    public List<REAL> asList() {
        List<REAL> ret = new ArrayList<REAL>();
        for(NODE node : vector) {
            ret.add(node.value());
        }
        return Collections.unmodifiableList(ret);
    }

    // TODO: Try to eliminate the need for this method when we have moved FileAcquirer to the config library
    // It is needed now because the builder has a list of String, while REAL=FileReference.
    public static LeafNodeVector<FileReference, FileNode> createFileNodeVector(Collection<String> values) {
        List<FileReference> fileReferences = new ArrayList<FileReference>();
         for (String s : values)
             fileReferences.add(new FileReference(ReferenceNode.stripQuotes(s)));

        return new LeafNodeVector<FileReference, FileNode>(fileReferences, new FileNode());
    }

    public static LeafNodeVector<Path, PathNode> createPathNodeVector(Collection<FileReference> values) {
        List<Path> paths = new ArrayList<>();
        for (FileReference fileReference : values)
            paths.add(Paths.get(fileReference.value()));

        return new LeafNodeVector<Path, PathNode>(paths, new PathNode());
    }
}

@@ -477,208 +477,93 @@
     
-NODE defaultNode;%0A%0A    /**%0A     * Creates a new vector with the given default node.%0A     */%0A    // TODO: remove this ctor when the library uses reflection via builders, and resizing won't be necessary
+private final List%3CREAL%3E realValues;%0A%0A    // TODO: take class instead of default node
 %0A   
@@ -585,16 +585,35 @@
 eVector(
+List%3CREAL%3E values, 
 NODE def
@@ -711,48 +711,8 @@
     
-this.defaultNode = defaultNode;%0A        
 if (
@@ -721,16 +721,27 @@
 eateNew(
+defaultNode
 ) == nul
@@ -844,200 +844,8 @@
   %7D%0A
-    %7D%0A%0A    // TODO: take class instead of default node when the library uses reflection via builders%0A    public LeafNodeVector(List%3CREAL%3E values, NODE defaultNode) %7B%0A        this(defaultNode);
 %0A   
@@ -911,16 +911,27 @@
 eateNew(
+defaultNode
 );%0A     
@@ -993,24 +993,63 @@
 ;%0A        %7D%0A
+        realValues = realList(vector);%0A
     %7D%0A%0A    /
@@ -1176,16 +1176,32 @@
 eateNew(
+NODE defaultNode
 ) %7B%0A    
@@ -1256,160 +1256,52 @@
     
-// TODO: create unmodifiable list in ctor when the library uses reflection via builders%0A    @SuppressWarnings(%22unchecked%22)%0A    public List%3CREAL%3E asList(
+private List%3CREAL%3E realList(List%3CNODE%3E nodes
 ) %7B%0A
@@ -1321,17 +1321,19 @@
 REAL%3E re
-t
+als
  = new A
@@ -1398,17 +1398,19 @@
       re
-t
+als
 .add(nod
@@ -1481,10 +1481,114 @@
 t(re
-t)
+als);%0A    %7D%0A%0A    @SuppressWarnings(%22unchecked%22)%0A    public List%3CREAL%3E asList() %7B%0A        return realValues
 ;%0A  

Refactor LeafNodeVector