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

br.com.objectos.http.Path Maven / Gradle / Ivy

There is a newer version: 0.2.1
Show newest version
/*
 * Copyright 2016 Objectos, Fábrica de Software LTDA.
 *
 * 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 br.com.objectos.http;

import java.util.NoSuchElementException;

/**
 * @author [email protected] (Marcio Endo)
 */
class Path {

  private final String[] parts;

  private int index;
  private boolean available;

  private Path(String[] parts) {
    this.parts = parts;
  }

  public static Path parse(String src) {
    String[] parts = src.split("/");
    return new Path(parts);
  }

  public boolean hasNextPart() {
    if (!available) {
      computeNextPart();
    }
    return available;
  }

  public String nextPart() {
    if (!hasNextPart()) {
      throw new NoSuchElementException();
    }

    String nextPart = parts[index];
    index++;
    available = false;
    return nextPart;
  }

  public int nextPartAsInt() throws NumberFormatException {
    String nextPart = nextPart();
    return Integer.parseInt(nextPart);
  }

  public Path reset() {
    index = 0;
    available = false;
    return this;
  }

  @Override
  public String toString() {
    StringBuilder sb = new StringBuilder();

    int last = parts.length - 1;
    for (int i = index; i < last; i++) {
      String current = parts[i];
      if (!current.equals("")) {
        sb.append(current);
        sb.append('/');
      }
    }

    if (index <= last) {
      sb.append(parts[last]);
    }

    return sb.toString();
  }

  private void computeNextPart() {
    while (index < parts.length) {
      String nextPart = parts[index];

      if (nextPart != null && !nextPart.equals("")) {
        available = true;
        break;
      }

      index++;
    }
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy