/*
 * 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 AttributeCountAggregator extends PropertyValueAggregator {

    /**
     * Map to aggregate property keys in.
     */
    private HashMap<String, Integer> attributeCount;

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

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

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

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

    @Override
    protected PropertyValue getAggregateInternal() {
        HashSet<String> returnAttributeSet = new HashSet<>();
        attributeCount.keySet().stream().forEach(a -> returnAttributeSet
                .add(a + "@" + attributeCount.get(a)));
        return PropertyValue.create(returnAttributeSet.stream()
                .collect(Collectors.joining(";")));
    }

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

}

@@ -1428,22 +1428,64 @@
 utes%22);%0A
+        attributeCount = new HashMap%3C%3E();%0A
     %7D%0A
-
 %0A    @Ov
@@ -1550,59 +1550,12 @@
 urn 
-attributeCount != null && !attributeCount.isEmpty()
+true
 ;%0A  
@@ -1636,191 +1636,8 @@
 ) %7B%0A
-        String str = in.getString();%0A        attributeCount = new HashMap%3C%3E();%0A        Arrays.asList(str.split(%22;%22)).stream()%0A                .forEach(a -%3E attributeCount.put(a, 1));%0A
     
@@ -1766,14 +1766,14 @@
 ays.
-asList
+stream
 (str
@@ -1785,25 +1785,16 @@
 t(%22;%22)).
-stream().
 forEach(
@@ -2319,43 +2319,8 @@
 ) %7B%0A
-        if (isInitialized()) %7B%0A    
     
@@ -2351,18 +2351,8 @@
 ();%0A
-        %7D%0A
     

Fix off-by-one bug and small optimizations