// Path: src/main/java/org/calrissian/flowmix/api/Flow.java
// public class Flow implements Serializable{
// 
//     String id;
//     String name;
//     String description;
// 
//     Map<String,StreamDef> streams = new HashMap<String, StreamDef>();
// 
//     public String getId() {
//         return id;
//     }
// 
//     public String getName() {
//         return name;
//     }
// 
//     public String getDescription() {
//         return description;
//     }
// 
//     public Collection<StreamDef> getStreams() {
//         return streams.values();
//     }
// 
//     public StreamDef getStream(String name) {
//         return streams.get(name);
//     }
// 
//     public void setId(String id) {
//         this.id = id;
//     }
// 
//     public void setName(String name) {
//         this.name = name;
//     }
// 
//     public void setDescription(String description) {
//         this.description = description;
//     }
// 
//     public void setStreams(List<StreamDef> streams) {
//         for(StreamDef def : streams)
//             this.streams.put(def.getName(), def);
//     }
// 
//     @Override
//     public boolean equals(Object o) {
//         if (this == o) return true;
//         if (o == null || getClass() != o.getClass()) return false;
// 
//         Flow flow = (Flow) o;
// 
//         if (description != null ? !description.equals(flow.description) : flow.description != null) return false;
//         if (id != null ? !id.equals(flow.id) : flow.id != null) return false;
//         if (name != null ? !name.equals(flow.name) : flow.name != null) return false;
//         if (streams != null ? !streams.equals(flow.streams) : flow.streams != null) return false;
// 
//         return true;
//     }
// 
//     @Override
//     public int hashCode() {
//         int result = id != null ? id.hashCode() : 0;
//         result = 31 * result + (name != null ? name.hashCode() : 0);
//         result = 31 * result + (description != null ? description.hashCode() : 0);
//         result = 31 * result + (streams != null ? streams.hashCode() : 0);
//         return result;
//     }
// 
//     @Override
//     public String toString() {
//         return "Flow{" +
//                 "id='" + id + '\'' +
//                 ", name='" + name + '\'' +
//                 ", description='" + description + '\'' +
//                 ", streams=" + streams +
//                 '}';
//     }
// }
// 
// Path: src/main/java/org/calrissian/flowmix/core/model/op/FlowOp.java
// public interface FlowOp extends Serializable {
// 
//     String getComponentName();
// }
// 
// Path: src/main/java/org/calrissian/flowmix/core/Constants.java
// public static final String OUTPUT = "output";


import java.util.Collection;
import java.util.List;
import java.util.SortedSet;
import java.util.TreeSet;
import org.calrissian.flowmix.api.Flow;
import org.calrissian.flowmix.core.model.op.FlowOp;
import org.calrissian.mango.domain.Tuple;
import org.calrissian.mango.domain.event.Event;
import org.calrissian.mango.types.TypeRegistry;
import static org.apache.commons.lang.StringUtils.join;
import static org.calrissian.flowmix.core.Constants.OUTPUT;
import static org.calrissian.mango.types.LexiTypeEncoders.LEXI_TYPES;
/*
 * Copyright (C) 2014 The Calrissian Authors
 *
 * 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 org.calrissian.flowmix.core.support;




public class Utils {

    private static final TypeRegistry<String> registry = LEXI_TYPES;

    private Utils() {}

  public static String buildKeyIndexForEvent(Event event, List<String> groupBy) {
    StringBuffer stringBuffer = new StringBuffer();

    if(groupBy == null || groupBy.size() == 0)
      return stringBuffer.toString();  // default partition when no groupBy fields are specified.

    for(String groupField : groupBy) {
      Collection<Tuple> tuples = event.getAll(groupField);
      SortedSet<String> values = new TreeSet<String>();

      if(tuples == null) {
        values.add("");
      } else {
        for(Tuple tuple : tuples)
          values.add(registry.encode(tuple.getValue()));
      }
      stringBuffer.append(groupField + join(values, "") + "|");
    }
    try {
      return stringBuffer.toString();
    } catch (Exception e) {
      return null;
    }
  }


  public static String buildKeyIndexForEvent(String flowId, Event event, List<String> groupBy) {
      return flowId + buildKeyIndexForEvent(event, groupBy);
  }


// Path: src/main/java/org/calrissian/flowmix/api/Flow.java
// public class Flow implements Serializable{
// 
//     String id;
//     String name;
//     String description;
// 
//     Map<String,StreamDef> streams = new HashMap<String, StreamDef>();
// 
//     public String getId() {
//         return id;
//     }
// 
//     public String getName() {
//         return name;
//     }
// 
//     public String getDescription() {
//         return description;
//     }
// 
//     public Collection<StreamDef> getStreams() {
//         return streams.values();
//     }
// 
//     public StreamDef getStream(String name) {
//         return streams.get(name);
//     }
// 
//     public void setId(String id) {
//         this.id = id;
//     }
// 
//     public void setName(String name) {
//         this.name = name;
//     }
// 
//     public void setDescription(String description) {
//         this.description = description;
//     }
// 
//     public void setStreams(List<StreamDef> streams) {
//         for(StreamDef def : streams)
//             this.streams.put(def.getName(), def);
//     }
// 
//     @Override
//     public boolean equals(Object o) {
//         if (this == o) return true;
//         if (o == null || getClass() != o.getClass()) return false;
// 
//         Flow flow = (Flow) o;
// 
//         if (description != null ? !description.equals(flow.description) : flow.description != null) return false;
//         if (id != null ? !id.equals(flow.id) : flow.id != null) return false;
//         if (name != null ? !name.equals(flow.name) : flow.name != null) return false;
//         if (streams != null ? !streams.equals(flow.streams) : flow.streams != null) return false;
// 
//         return true;
//     }
// 
//     @Override
//     public int hashCode() {
//         int result = id != null ? id.hashCode() : 0;
//         result = 31 * result + (name != null ? name.hashCode() : 0);
//         result = 31 * result + (description != null ? description.hashCode() : 0);
//         result = 31 * result + (streams != null ? streams.hashCode() : 0);
//         return result;
//     }
// 
//     @Override
//     public String toString() {
//         return "Flow{" +
//                 "id='" + id + '\'' +
//                 ", name='" + name + '\'' +
//                 ", description='" + description + '\'' +
//                 ", streams=" + streams +
//                 '}';
//     }
// }
// 
// Path: src/main/java/org/calrissian/flowmix/core/model/op/FlowOp.java
// public interface FlowOp extends Serializable {
// 
//     String getComponentName();
// }
// 
// Path: src/main/java/org/calrissian/flowmix/core/Constants.java
// public static final String OUTPUT = "output";


// Path: src/main/java/org/calrissian/flowmix/core/support/Utils.java
import java.util.Collection;
import java.util.List;
import java.util.SortedSet;
import java.util.TreeSet;
import org.calrissian.flowmix.api.Flow;
import org.calrissian.flowmix.core.model.op.FlowOp;
import org.calrissian.mango.domain.Tuple;
import org.calrissian.mango.domain.event.Event;
import org.calrissian.mango.types.TypeRegistry;
import static org.apache.commons.lang.StringUtils.join;
import static org.calrissian.flowmix.core.Constants.OUTPUT;
import static org.calrissian.mango.types.LexiTypeEncoders.LEXI_TYPES;

/*
 * Copyright (C) 2014 The Calrissian Authors
 *
 * 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 org.calrissian.flowmix.core.support;




public class Utils {

    private static final TypeRegistry<String> registry = LEXI_TYPES;

    private Utils() {}

  public static String buildKeyIndexForEvent(Event event, List<String> groupBy) {
    StringBuffer stringBuffer = new StringBuffer();

    if(groupBy == null || groupBy.size() == 0)
      return stringBuffer.toString();  // default partition when no groupBy fields are specified.

    for(String groupField : groupBy) {
      Collection<Tuple> tuples = event.getAll(groupField);
      SortedSet<String> values = new TreeSet<String>();

      if(tuples == null) {
        values.add("");
      } else {
        for(Tuple tuple : tuples)
          values.add(registry.encode(tuple.getValue()));
      }
      stringBuffer.append(groupField + join(values, "") + "|");
    }
    try {
      return stringBuffer.toString();
    } catch (Exception e) {
      return null;
    }
  }


  public static String buildKeyIndexForEvent(String flowId, Event event, List<String> groupBy) {
      return flowId + buildKeyIndexForEvent(event, groupBy);
  }


  public static String getNextStreamFromFlowInfo(Flow flow, String streamName, int idx) {