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

swim.uri.UriMapper Maven / Gradle / Ivy

There is a newer version: 3.10.0
Show newest version
// Copyright 2015-2019 SWIM.AI 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 swim.uri;

import java.util.AbstractCollection;
import java.util.AbstractSet;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import swim.codec.Debug;
import swim.codec.Format;
import swim.codec.Output;

public abstract class UriMapper implements Iterable>, Map, Debug {
  UriMapper() {
    // stub
  }

  @Override
  public abstract boolean isEmpty();

  @Override
  public abstract int size();

  @Override
  public boolean containsKey(Object key) {
    return get(key) != null;
  }

  @Override
  public abstract boolean containsValue(Object value);

  public abstract T get(Uri uri);

  public T get(String uri) {
    return get(Uri.parse(uri));
  }

  @Override
  public T get(Object key) {
    if (key instanceof Uri) {
      return get((Uri) key);
    } else if (key instanceof String) {
      return get((String) key);
    } else {
      return null;
    }
  }

  @Override
  public T put(Uri pattern, T value) {
    throw new UnsupportedOperationException();
  }

  @Override
  public void putAll(Map map) {
    throw new UnsupportedOperationException();
  }

  @Override
  public T remove(Object key) {
    throw new UnsupportedOperationException();
  }

  @Override
  public void clear() {
    throw new UnsupportedOperationException();
  }

  public abstract UriMapper merged(UriMapper that);

  public UriMapper updated(Uri pattern, T value) {
    return merged(from(pattern, value));
  }

  public UriMapper updated(UriPattern pattern, T value) {
    return updated(pattern.toUri(), value);
  }

  public UriMapper updated(String pattern, T value) {
    return updated(Uri.parse(pattern), value);
  }

  public abstract UriMapper removed(Uri pattern);

  public UriMapper removed(UriPattern pattern) {
    return removed(pattern.toUri());
  }

  public UriMapper removed(String pattern) {
    return removed(Uri.parse(pattern));
  }

  public abstract UriMapper unmerged(UriMapper that);

  @Override
  public Set> entrySet() {
    return new UriMapperEntrySet(this);
  }

  @Override
  public Set keySet() {
    return new UriMapperKeySet(this);
  }

  @Override
  public Collection values() {
    return new UriMapperValues(this);
  }

  @Override
  public abstract Iterator> iterator();

  public abstract Iterator keyIterator();

  public abstract Iterator valueIterator();

  @Override
  public void debug(Output output) {
    output = output.write("UriMapper").write('.').write("empty").write('(').write(')');
    for (Entry from : this) {
      output = output.write('.').write("updated").write('(')
          .debug(from.getKey().toString()).write(", ")
          .debug(from.getValue()).write(')');
    }
  }

  @Override
  public String toString() {
    return Format.debug(this);
  }

  private static UriMapper empty;

  @SuppressWarnings("unchecked")
  public static  UriMapper empty() {
    if (empty == null) {
      empty = new UriEmptyMapping();
    }
    return (UriMapper) empty;
  }

  public static  UriMapper from(Uri pattern, T value) {
    return UriSchemeMapper.compile(pattern, pattern.scheme(), pattern.authority(), pattern.path(), pattern.query(), pattern.fragment(), value);
  }

  public static  UriMapper from(UriPattern pattern, T value) {
    return from(pattern.toUri(), value);
  }

  public static  UriMapper from(String uriString, T value) {
    return from(Uri.parse(uriString), value);
  }
}

final class UriMapperEntrySet extends AbstractSet> {
  final UriMapper mapper;

  UriMapperEntrySet(UriMapper mapper) {
    this.mapper = mapper;
  }

  @Override
  public boolean isEmpty() {
    return this.mapper.isEmpty();
  }

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

  @Override
  public Iterator> iterator() {
    return this.mapper.iterator();
  }
}

final class UriMapperKeySet extends AbstractSet {
  final UriMapper mapper;

  UriMapperKeySet(UriMapper mapper) {
    this.mapper = mapper;
  }

  @Override
  public boolean isEmpty() {
    return this.mapper.isEmpty();
  }

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

  @Override
  public Iterator iterator() {
    return this.mapper.keyIterator();
  }
}

final class UriMapperValues extends AbstractCollection {
  final UriMapper mapper;

  UriMapperValues(UriMapper mapper) {
    this.mapper = mapper;
  }

  @Override
  public boolean isEmpty() {
    return this.mapper.isEmpty();
  }

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

  @Override
  public Iterator iterator() {
    return this.mapper.valueIterator();
  }
}

final class UriEmptyMapping extends UriTerminalMapper {
  @Override
  public boolean isEmpty() {
    return true;
  }

  @Override
  public int size() {
    return 0;
  }

  @Override
  public boolean containsValue(Object value) {
    return false;
  }

  @Override
  public T get() {
    return null;
  }

  @Override
  public Set> entrySet() {
    return Collections.emptySet();
  }

  @Override
  public Set keySet() {
    return Collections.emptySet();
  }

  @Override
  public Collection values() {
    return Collections.emptyList();
  }

  @Override
  public Iterator> iterator() {
    return Collections.emptyIterator();
  }

  @Override
  public Iterator keyIterator() {
    return Collections.emptyIterator();
  }

  @Override
  public Iterator valueIterator() {
    return Collections.emptyIterator();
  }
}