All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.datastax.oss.protocol.internal.util.IntIntMap Maven / Gradle / Ivy

/*
 * Copyright DataStax, 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.datastax.oss.protocol.internal.util;

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

/** An {@link IntMap} containing other {@code IntMap}s. */
public class IntIntMap {

  public static  Builder builder() {
    return new Builder<>();
  }

  private final IntMap> outer;

  private IntIntMap(IntMap> outer) {
    this.outer = outer;
  }

  public V get(int key1, int key2) {
    IntMap inner = outer.get(key1);
    return (inner == null) ? null : inner.get(key2);
  }

  public static class Builder {
    private Map> innerBuilders = new HashMap<>();

    public Builder put(int key1, int key2, V value) {
      innerBuilders.computeIfAbsent(key1, k -> IntMap.builder()).put(key2, value);
      return this;
    }

    public IntIntMap build() {
      IntMap.Builder> outerBuilder = IntMap.builder();
      for (Map.Entry> entry : innerBuilders.entrySet()) {
        outerBuilder.put(entry.getKey(), entry.getValue().build());
      }
      return new IntIntMap<>(outerBuilder.build());
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy