001/* 002 * ModeShape (http://www.modeshape.org) 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.modeshape.connector.git; 017 018import org.modeshape.common.annotation.Immutable; 019 020/** 021 * 022 */ 023@Immutable 024public final class CallSpecification { 025 026 protected static final String DELIMITER_STR = "/"; 027 protected static final char DELIMITER = DELIMITER_STR.charAt(0); 028 029 private final String id; 030 private final String[] parts; 031 private final int numParts; 032 private final int parameterCount; 033 034 public CallSpecification( String id ) { 035 this.id = id; 036 String relative = id.replaceFirst("^[/]", ""); 037 this.parts = relative.split("/"); 038 this.numParts = this.parts.length; 039 this.parameterCount = numParts - 1; 040 assert this.numParts > 0; 041 } 042 043 public String getFunctionName() { 044 return this.parts[0]; 045 } 046 047 /** 048 * The identifier of this call. 049 * 050 * @return the identifier; never null 051 */ 052 public String getId() { 053 return id; 054 } 055 056 public int parameterCount() { 057 return parameterCount; 058 } 059 060 /** 061 * Get the specified parameter. 062 * 063 * @param index the 0-based index of the parameter, excluding the first segment that is the name 064 * @return the parameter 065 */ 066 public String parameter( int index ) { 067 return parts[index + 1]; 068 } 069 070 public String parametersAsPath( int fromIndex ) { 071 return parametersAsPath(fromIndex, numParts); 072 } 073 074 public String parametersAsPath( int fromIndex, 075 int toIndex ) { 076 ++fromIndex; // ignore the name 077 assert fromIndex < numParts; 078 assert toIndex <= numParts; 079 StringBuilder sb = new StringBuilder(); 080 for (int i = fromIndex; i != toIndex; ++i) { 081 sb.append(DELIMITER); 082 sb.append(parts[i]); 083 } 084 return sb.toString(); 085 } 086 087 public String lastParameter() { 088 if (numParts == 0) return null; 089 return this.parts[numParts - 1]; 090 } 091 092 public String childId( String childPart ) { 093 return id + DELIMITER + childPart; 094 } 095 096 public String getParentId() { 097 if (numParts == 0) { 098 // There is no parent ... 099 return ""; 100 } 101 if (numParts == 1) { 102 // The parent is the root ... 103 return GitRoot.ID; 104 } 105 StringBuilder sb = new StringBuilder(); 106 int length = numParts - 1; 107 for (int i = 0; i != length; ++i) { 108 String part = parts[i]; 109 sb.append(DELIMITER); 110 sb.append(part); 111 } 112 return sb.toString(); 113 } 114 115 @Override 116 public String toString() { 117 return id; 118 } 119}