/*
 * Copyright 2016 higherfrequencytrading.com
 *
 * 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 net.openhft.chronicle.wire;

import net.openhft.chronicle.core.util.CharSequenceComparator;
import org.jetbrains.annotations.NotNull;

import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

/**
 * A simple parser to associate actions based on events/field names received.
 */
public class VanillaWireParser<O> implements WireParser<O> {
    private final Map<CharSequence, WireParselet<O>> namedConsumer = new TreeMap<>(CharSequenceComparator.INSTANCE);
    private final Map<Integer, WireParselet<O>> numberedConsumer = new HashMap<>();
    private final WireParselet<O> defaultConsumer;
    private final StringBuilder sb = new StringBuilder(128);

    public VanillaWireParser(WireParselet<O> defaultConsumer) {
        this.defaultConsumer = defaultConsumer;
    }

    @Override
    public WireParselet<O> getDefaultConsumer() {
        return defaultConsumer;
    }

    public void parseOne(@NotNull WireIn wireIn, O out) {
        @NotNull ValueIn valueIn = wireIn.readEventName(sb);
        WireParselet<O> consumer = lookup(sb);
        if (consumer == null)
            consumer = getDefaultConsumer();
        consumer.accept(sb, valueIn, out);
    }

    @NotNull
    @Override
    public VanillaWireParser<O> register(@NotNull WireKey key, WireParselet<O> valueInConsumer) {
        namedConsumer.put(key.name(), valueInConsumer);
        numberedConsumer.put(key.code(), valueInConsumer);
        return this;
    }

    @Override
    public WireParselet<O> lookup(CharSequence name) {
        return namedConsumer.get(name);
    }

    @Override
    public WireParselet<O> lookup(int number) {
        return numberedConsumer.get(number);
    }
}

@@ -703,16 +703,68 @@
 arator;%0A
+import net.openhft.chronicle.core.util.StringUtils;%0A
 import o
@@ -797,16 +797,16 @@
 otNull;%0A
-
 %0Aimport 
@@ -1332,16 +1332,136 @@
 er(128);
+%0A    private final StringBuilder lastEventName = new StringBuilder(128);%0A    private WireParselet%3CO%3E lastParslet = null;
 %0A%0A    pu
@@ -1558,32 +1558,79 @@
 efaultConsumer;%0A
+        lastEventName.appendCodePoint(0xFFFF);%0A
     %7D%0A%0A    @Over
@@ -1866,24 +1866,264 @@
 elet%3CO%3E 
-consumer
+parslet;%0A        // on the assumption most messages are the same as the last,%0A        // save having to lookup a TreeMap.%0A        if (StringUtils.isEqual(sb, lastEventName)) %7B%0A            parslet = lastParslet;%0A%0A        %7D else %7B%0A            parslet
  = looku
@@ -2141,20 +2141,23 @@
     
-if (consumer
+    if (parslet
  == 
@@ -2174,24 +2174,27 @@
         
+    
-consumer
+parslet
  = getDe
@@ -2222,16 +2222,26 @@
     
-consumer
+%7D%0A%0A        parslet
 .acc
@@ -2263,16 +2263,117 @@
 , out);%0A
+        lastEventName.setLength(0);%0A        lastEventName.append(sb);%0A        lastParslet = parslet;%0A
     %7D%0A%0A 

Use a single value cache to avoid looking up a TreeMap.