/**
 *    Retz
 *    Copyright (C) 2016-2017 Nautilus Technologies, Inc.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */
package io.github.retz.planner;

import io.github.retz.planner.spi.Attribute;
import io.github.retz.protocol.data.Range;
import org.apache.mesos.Protos;

import java.util.LinkedList;
import java.util.List;

public class AttributeBuilder {
    private List<Attribute> list = new LinkedList<>();

    public AttributeBuilder(List<Protos.Attribute> attrs) {
        for (Protos.Attribute attr : attrs) {

            Attribute.Type t;
            Object o;
            if (attr.hasScalar()) {
                t = Attribute.Type.SCALAR;
                o = Double.valueOf(attr.getScalar().getValue());
            } else if (attr.hasRanges()) {
                t = Attribute.Type.RANGES;
                List<Range> ranges = new LinkedList<>();
                for (Protos.Value.Range range : attr.getRanges().getRangeList()) {
                    Range r = new Range(range.getBegin(), range.getEnd());
                    ranges.add(r);
                }
                o = ranges;
            } else if (attr.hasSet()) {
                t = Attribute.Type.SET;
                o = attr.getSet();
            } else if (attr.hasText()) {
                t = Attribute.Type.TEXT;
                o = attr.getText();
            } else {
                throw new AssertionError("attribute has unknown type: " + attr.getType());
            }
            Attribute a = new Attribute(attr.getName(), t, o);
            list.add(a);
        }
    }

    public List<Attribute> build() {
        return list;
    }
}

@@ -809,16 +809,42 @@
 rotos;%0A%0A
+import java.util.HashSet;%0A
 import j
@@ -886,16 +886,38 @@
 il.List;
+%0Aimport java.util.Set;
 %0A%0Apublic
@@ -1189,24 +1189,62 @@
 Scalar()) %7B%0A
+                // converts to Double%0A
             
@@ -1378,24 +1378,67 @@
 Ranges()) %7B%0A
+                // converts to List%3CRange%3E%0A
             
@@ -1800,24 +1800,67 @@
 hasSet()) %7B%0A
+                // converts to Set%3CString%3E%0A
             
@@ -1895,33 +1895,41 @@
                 
-o
+int count
  = attr.getSet()
@@ -1928,16 +1928,236 @@
 getSet()
+.getItemCount();%0A                Set%3CString%3E items = new HashSet%3C%3E();%0A                for (int i = 0; i %3C count; i++) %7B%0A                    items.add(attr.getSet().getItem(i));%0A                %7D%0A                o = items
 ;%0A      
@@ -2187,24 +2187,62 @@
 asText()) %7B%0A
+                // converts to String%0A
             
@@ -2304,16 +2304,27 @@
 etText()
+.getValue()
 ;%0A      

convert Attribute values to correct types