001package org.clafer.ontology; 002 003import java.util.Arrays; 004 005/** 006 * 007 * @author jimmy 008 */ 009public class Path { 010 011 private final Concept[] steps; 012 013 public Path(Concept... steps) { 014 this.steps = steps; 015 } 016 017 public Concept getContext() { 018 return steps[0]; 019 } 020 021 public Concept[] getSteps() { 022 return steps; 023 } 024 025 public int length() { 026 return steps.length; 027 } 028 029 protected Path newPath(Concept... steps) { 030 return new Path(steps); 031 } 032 033 public Path prepend(Concept step) { 034 Concept[] newSteps = new Concept[steps.length + 1]; 035 newSteps[0] = step; 036 System.arraycopy(steps, 0, newSteps, 1, steps.length); 037 return newPath(newSteps); 038 } 039 040 public Path append(Concept step) { 041 Concept[] newSteps = Arrays.copyOf(steps, steps.length + 1); 042 newSteps[steps.length] = step; 043 return newPath(newSteps); 044 } 045 046 public Path replaceContext(Concept newContext) { 047 Concept[] newSteps = Arrays.copyOf(steps, steps.length); 048 newSteps[0] = newContext; 049 return newPath(steps); 050 } 051 052 public Path dropPrefix(int index) { 053 if (index == 0) { 054 return this; 055 } 056 return newPath(Arrays.copyOfRange(steps, index, steps.length)); 057 } 058 059 @Override 060 public boolean equals(Object obj) { 061 if (obj instanceof Path) { 062 Path other = (Path) obj; 063 return Arrays.equals(steps, other.steps); 064 } 065 return false; 066 } 067 068 @Override 069 public int hashCode() { 070 return Arrays.hashCode(steps); 071 } 072 073 @Override 074 public String toString() { 075 StringBuilder result = new StringBuilder(); 076 result.append(steps[0]); 077 for (int i = 1; i < steps.length; i++) { 078 result.append('.').append(steps[i]); 079 } 080 return result.toString(); 081 } 082}