/*
 * Copyright 2013 Netflix, 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 com.netflix.suro.routing;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.inject.Singleton;
import com.netflix.suro.message.MessageContainer;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.Map;

/**
 * A collection of {@link RoutingInfo}, each of which is mapped to a routing key.
 * 
 * @author jbae
 * @author elandau
 *
 */
@Singleton
public class RoutingMap {
    public static class Route {
        private final String sink;
        private final Filter filter;
        
        @JsonCreator
        public Route(
               @JsonProperty("sink") String sink,
               @JsonProperty("filter") Filter filter) {
            this.sink   = sink;
            this.filter = filter;
        }
        
        public String getSink() {
            return sink;
        }
        
        public Filter getFilter() {
            return filter;
        }
        
        public boolean doFilter(MessageContainer message) throws Exception {
            return filter != null ? filter.doFilter(message) : true;
        }

    }
    
    public static class RoutingInfo {
        private final List<Route> where;
        private final Filter filter;

        @JsonCreator
        public RoutingInfo(
                @JsonProperty("where") List<Route> where,
                @JsonProperty("filter") Filter filter
        ) {
            this.where  = where;
            this.filter = filter;
        }

        public List<Route> getWhere() { 
            return where; 
        }

        /**
         * Filters given messages based on filters defined in
         * this object.
         * @param message the message to be filtered.
         * @return true if there is no filter found or if the filter returns true when applied to the message.
         * @throws Exception thrown if filtering ran into an unexpected failure
         */
        public boolean doFilter(MessageContainer message) throws Exception {
            return filter == null || filter.doFilter(message);
        }
    }
    
    private volatile Map<String, RoutingInfo> routingMap = Maps.newHashMap();

    public RoutingInfo getRoutingInfo(String routingKey) {
        return routingMap.get(routingKey);
    }

    public void set(Map<String, RoutingInfo> routes) {
        this.routingMap = ImmutableMap.copyOf(routes);
    }
}

@@ -1860,17 +1860,18 @@
 ter 
-!
+=
 = null 
-?
+%7C%7C
  fil
@@ -1895,15 +1895,8 @@
 age)
- : true
 ;%0A  

Simplified a conditional statement