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

com.ibm.wala.util.intset.OffsetOrdinalSetMapping Maven / Gradle / Ivy

There is a newer version: 1.6.6
Show newest version
/*
 * Copyright (c) 2007 IBM Corporation.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 */
package com.ibm.wala.util.intset;

import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.stream.Stream;

/** An ordinal set mapping, backed a delegate, but adding an offset to each index. */
public class OffsetOrdinalSetMapping implements OrdinalSetMapping {

  private final OrdinalSetMapping delegate;

  private final int offset;

  private OffsetOrdinalSetMapping(OrdinalSetMapping delegate, int offset) {
    this.delegate = delegate;
    this.offset = offset;
  }

  @Override
  public int getMaximumIndex() {
    return offset + delegate.getMaximumIndex();
  }

  @Override
  public int getSize() {
    return delegate.getSize();
  }

  public static  OffsetOrdinalSetMapping make(OrdinalSetMapping delegate, int offset) {
    if (delegate == null) {
      throw new IllegalArgumentException("null delegate");
    }
    return new OffsetOrdinalSetMapping<>(delegate, offset);
  }

  public static  OffsetOrdinalSetMapping make(int offset) {
    MutableMapping m = MutableMapping.make();
    return new OffsetOrdinalSetMapping<>(m, offset);
  }

  @Override
  public int add(T o) {
    return offset + delegate.add(o);
  }

  @Override
  public int getMappedIndex(Object o) {
    if (delegate.getMappedIndex(o) == -1) {
      return -1;
    }
    return offset + delegate.getMappedIndex(o);
  }

  @Override
  public T getMappedObject(int n) throws NoSuchElementException {
    return delegate.getMappedObject(n - offset);
  }

  @Override
  public boolean hasMappedIndex(T o) {
    return delegate.hasMappedIndex(o);
  }

  @Override
  public Iterator iterator() {
    return delegate.iterator();
  }

  @Override
  public Stream stream() {
    return delegate.stream();
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy