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

nstream.adapter.common.relay.SimpleDynamicUri Maven / Gradle / Ivy

There is a newer version: 4.14.22
Show newest version
// Copyright 2015-2024 Nstream, inc.
//
// Licensed under the Redis Source Available License 2.0 (RSALv2) Agreement;
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     https://redis.com/legal/rsalv2-agreement/
//
// 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 nstream.adapter.common.relay;

import java.util.ArrayList;
import java.util.List;
import swim.recon.Recon;
import swim.structure.Item;
import swim.uri.UriPath;

class SimpleDynamicUri {

  final List components;

  SimpleDynamicUri(String schema) {
    this.components = new ArrayList<>(2 * (int) schema.chars().filter(ch -> ch == '/').count() + 3);
    for (int i = 0; i < schema.length();) {
      i = addPathComponent(schema, i, this.components);
    }
  }

  String evaluate(Item scope) {
    final String[] components = new String[this.components.size()];
    for (int i = 0; i < this.components.size(); i++) {
      final String s = this.components.get(i);
      if (s.contains("$")) {
        components[i] = Recon.parse(s).evaluate(scope).stringValue();
      } else {
        components[i] = s;
      }
    }
    return UriPath.of(components).toString();
  }

  private static int addPathComponent(String schema, int i, List components) {
    if (schema.charAt(i) == '/') {
      components.add("/");
      return i + 1;
    } else {
      final int j = nextSeparatorIdx(schema, i);
      if (j > i) {
        components.add(schema.substring(i, j));
      }
      return j;
    }
  }

  private static int nextSeparatorIdx(String schema, int i) {
    final int result = schema.indexOf("/", i);
    return result < 0 ? schema.length()
        : result == schema.length() - 1 ? result
        : schema.charAt(result + 1) == ' ' ? nextSeparatorIdx(schema, result + 1)
        : result;
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy