/*
 * Copyright 2017 TraderJoe95 <johannes.leupold@schie-le.de>.
 *
 * 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 one.p_f.testing.msagimport.grouping.aggregation;

import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.stream.Collectors;
import org.gradoop.common.model.impl.properties.PropertyValue;
import org.gradoop.flink.model.impl.operators.grouping.functions.aggregation.PropertyValueAggregator;

/**
 * Aggregates semicolon separated attribute lists using a Set.
 *
 * @author TraderJoe95 <johannes.leupold@schie-le.de>
 */
public class AttributeSetAggregator extends PropertyValueAggregator {

    /**
     * Set to aggregate property keys in.
     */
    private HashSet<String> attributeSet;

    private HashMap<String, Integer> attributeCount;

    /**
     * Creates a new <code>AttributeSetAggregator</code>.
     */
    public AttributeSetAggregator() {
        super("attributes", "attributes");
    }

    @Override
    protected boolean isInitialized() {
        return attributeSet != null && !attributeSet.isEmpty();
    }

    @Override
    protected void initializeAggregate(PropertyValue in) {
        String str = in.getString();
        attributeSet = new HashSet<>(Arrays.asList(str.split(";")));
        attributeCount = new HashMap<>();
        attributeSet.stream().forEach(a -> attributeCount.put(a, 1));
    }

    @Override
    protected void aggregateInternal(PropertyValue in) {
        String str = in.getString();
        HashSet<String> newAttributeSet
                = new HashSet<>(Arrays.asList(str.split(";")));
        attributeSet.addAll(newAttributeSet);
        newAttributeSet.stream().forEach(a -> attributeCount.put(a,
                attributeCount.getOrDefault(a, 0) + 1));
    }

    @Override
    protected PropertyValue getAggregateInternal() {
        return PropertyValue.create(attributeSet.stream()
                .map(a -> a + "@" + attributeCount.get(a))
                .collect(Collectors.joining(";")));
    }

    @Override
    public void resetAggregate() {
        if (isInitialized()) {
            attributeSet.clear();
            attributeCount.clear();
        }
    }

}

@@ -1172,19 +1172,19 @@
 %0A     * 
-Set
+Map
  to aggr
@@ -1219,51 +1219,8 @@
  */%0A
-    private HashSet%3CString%3E attributeSet;%0A%0A
     
@@ -1509,18 +1509,20 @@
 ttribute
-Se
+Coun
 t != nul
@@ -1536,18 +1536,20 @@
 ttribute
-Se
+Coun
 t.isEmpt
@@ -1687,18 +1687,20 @@
 ttribute
-Se
+Coun
 t = new 
@@ -1695,38 +1695,49 @@
 Count = new Hash
-Set%3C%3E(
+Map%3C%3E();%0A        
 Arrays.asList(st
@@ -1753,82 +1753,34 @@
 ;%22))
-);%0A        attributeCount = new HashMap%3C%3E();%0A        attributeSet.stream()
+.stream()%0A                
 .for
@@ -1943,173 +1943,37 @@
     
-HashSet%3CString%3E newAttributeSet%0A                = new HashSet%3C%3E(Arrays.asList(str.split(%22;%22)));%0A        attributeSet.addAll(newAttributeSet);%0A        newAttributeSet
+Arrays.asList(str.split(%22;%22))
 .str
@@ -2005,23 +2005,16 @@
 uteCount
-.put(a,
 %0A       
@@ -2013,32 +2013,40 @@
 %0A               
+ .put(a,
  attributeCount.
@@ -2157,48 +2157,93 @@
     
-return PropertyValue.create(attribute
+HashSet%3CString%3E returnAttributeSet = new HashSet%3C%3E();%0A        attributeCount.key
 Set
+()
 .str
@@ -2239,32 +2239,64 @@
 eySet().stream()
+.forEach(a -%3E returnAttributeSet
 %0A               
@@ -2301,17 +2301,12 @@
    .
-map(a -%3E 
+add(
 a + 
@@ -2333,16 +2333,82 @@
 .get(a))
+);%0A        return PropertyValue.create(returnAttributeSet.stream()
 %0A       
@@ -2543,42 +2543,8 @@
 ) %7B%0A
-            attributeSet.clear();%0A
     

Remove redundant attributeSet