/**
 * Copyright (C) 2012 Ness Computing, 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.nesscomputing.sequencer;

import java.util.AbstractList;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Nonnull;
import javax.annotation.concurrent.Immutable;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;

import gnu.trove.map.TObjectIntMap;
import gnu.trove.map.hash.TObjectIntHashMap;

/**
 * A Sequencer that is initialized upon construction and may never
 * change afterward.
 */
@Immutable
class ImmutableSequencerImpl<K> extends ImmutableSequencer<K>
{
    private static final long serialVersionUID = 1L;

    private final TObjectIntMap<K> forward;

    private final K[] reverse;

    public ImmutableSequencerImpl(@JsonProperty("forward") TObjectIntMap<K> forward, @JsonProperty("reverse") K[] reverse)
    {
        this.forward = forward;
        this.reverse = reverse;
    }

    @SuppressWarnings("unchecked")
    ImmutableSequencerImpl()
    {
        forward = new TObjectIntHashMap<>(0);
        reverse = (K[]) new Object[0];
    }

    @SuppressWarnings("unchecked")
    @JsonCreator
    ImmutableSequencerImpl(ArrayList<K> elements)
    {
        final int size = elements.size();
        forward = new TObjectIntHashMap<>(size);
        reverse = (K[]) new Object[size];
        int v = 0;
        for (K k : elements) {
            forward.put(k, v);
            reverse[v] = k;
            v++;
        }
    }

    @SuppressWarnings("unchecked")
    ImmutableSequencerImpl(Sequencer<K> sequencer)
    {
        final int size = sequencer.size();
        forward = new TObjectIntHashMap<>(size);
        reverse = (K[]) new Object[size];

        for (int v = 0; v < size; v++) {
            final K k = sequencer.unsequence(v);

            forward.put(k, v);
            reverse[v] = k;
        }
    }

    @Nonnull
    public static <K> ImmutableSequencerImpl<K> copyOf(@Nonnull Sequencer<K> seq)
    {
        if (seq instanceof ImmutableSequencerImpl) {
            return (ImmutableSequencerImpl<K>) seq;
        }
        return new ImmutableSequencerImpl<>(seq);
    }

    @Override
    protected int depth()
    {
        return 1;
    }

    @Override
    public List<K> getKeys()
    {
        return new ListKeyView();
    }

    @Override
    public boolean containsKey(Object key)
    {
        return forward.containsKey(key);
    }

    @Override
    public int sequenceIfExists(K key)
    {
        final int result = forward.get(key);
        if (result == forward.getNoEntryValue() && !containsKey(key)) {
            return -1;
        }
        return result;
    }

    @Override
    public void sequenceExisting(Iterable<K> keys, TObjectIntMap<K> result)
    {
        for (K key : keys) {
            int val = forward.get(key);
            if (val != forward.getNoEntryValue() || forward.containsKey(key)) {
                result.put(key, val);
            }
        }
    }

    @Override
    public K unsequence(int index)
    {
        return reverse[index];
    }

    @Override
    public int size()
    {
        return forward.size();
    }

    private class ListKeyView extends AbstractList<K>
    {
        @Override
        public K get(int index)
        {
            return ImmutableSequencerImpl.this.unsequence(index);
        }

        @Override
        public int size()
        {
            return ImmutableSequencerImpl.this.size();
        }
    }
}

@@ -671,36 +671,8 @@
 st;%0A
-import java.util.ArrayList;%0A
 impo
@@ -1184,16 +1184,67 @@
 ID = 1L;
+%0A    private static final float LOAD_FACTOR = 0.5f;
 %0A%0A    pr
@@ -1627,16 +1627,33 @@
 hMap%3C%3E(0
+, LOAD_FACTOR, -1
 );%0A     
@@ -1776,13 +1776,8 @@
 mpl(
-Array
 List
@@ -1876,32 +1876,49 @@
 ntHashMap%3C%3E(size
+, LOAD_FACTOR, -1
 );%0A        rever
@@ -2270,16 +2270,33 @@
 p%3C%3E(size
+, LOAD_FACTOR, -1
 );%0A     
@@ -3116,171 +3116,110 @@
     
-final int result = forward.get(key);%0A        if (result == forward.getNoEntryValue() && !containsKey(key)) %7B%0A            return -1;%0A        %7D%0A        return result
+assert forward.getNoEntryValue() == -1 : %22noEntryValue must be == -1%22;%0A        return forward.get(key)
 ;%0A  
@@ -3311,32 +3311,111 @@
 %3E result)%0A    %7B%0A
+        assert forward.getNoEntryValue() == -1 : %22noEntryValue must be == -1%22;%0A
         for (K k
@@ -3519,36 +3519,8 @@
 ue()
- %7C%7C forward.containsKey(key)
 ) %7B%0A

Improve ImmutableSequencerImpl efficiency by using -1 for the noEntryValue