Changeset View
Changeset View
Standalone View
Standalone View
java/server/src/main/java/org/softwareheritage/graph/SwhPath.java
package org.softwareheritage.graph; | package org.softwareheritage.graph; | ||||
import java.util.ArrayList; | import java.util.ArrayList; | ||||
import com.fasterxml.jackson.annotation.JsonValue; | import com.fasterxml.jackson.annotation.JsonValue; | ||||
import org.softwareheritage.graph.SwhId; | import org.softwareheritage.graph.SwhPID; | ||||
/** | /** | ||||
* Wrapper class to store a list of {@link SwhId}. | * Wrapper class to store a list of {@link SwhPID}. | ||||
* | * | ||||
* @author Thibault Allançon | * @author Thibault Allançon | ||||
* @version 0.0.1 | * @version 0.0.1 | ||||
* @since 0.0.1 | * @since 0.0.1 | ||||
* @see org.softwareheritage.graph.SwhId | * @see org.softwareheritage.graph.SwhPID | ||||
*/ | */ | ||||
public class SwhPath { | public class SwhPath { | ||||
/** Internal list of {@link SwhId} */ | /** Internal list of {@link SwhPID} */ | ||||
ArrayList<SwhId> path; | ArrayList<SwhPID> path; | ||||
/** | /** | ||||
* Constructor. | * Constructor. | ||||
*/ | */ | ||||
public SwhPath() { | public SwhPath() { | ||||
this.path = new ArrayList<SwhId>(); | this.path = new ArrayList<SwhPID>(); | ||||
} | } | ||||
/** | /** | ||||
* Constructor. | * Constructor. | ||||
* | * | ||||
* @param swhIds variable number of string PIDs to initialize this path with | * @param swhPIDs variable number of string PIDs to initialize this path with | ||||
*/ | */ | ||||
public SwhPath(String ...swhIds) { | public SwhPath(String ...swhPIDs) { | ||||
this(); | this(); | ||||
for (String swhId : swhIds) { | for (String swhPID : swhPIDs) { | ||||
add(new SwhId(swhId)); | add(new SwhPID(swhPID)); | ||||
} | } | ||||
} | } | ||||
/** | /** | ||||
* Constructor. | * Constructor. | ||||
* | * | ||||
* @param swhIds variable number of {@link SwhId} to initialize this path with | * @param swhPIDs variable number of {@link SwhPID} to initialize this path with | ||||
* @see org.softwareheritage.graph.SwhId | * @see org.softwareheritage.graph.SwhPID | ||||
*/ | */ | ||||
public SwhPath(SwhId ...swhIds) { | public SwhPath(SwhPID ...swhPIDs) { | ||||
this(); | this(); | ||||
for (SwhId swhId : swhIds) { | for (SwhPID swhPID : swhPIDs) { | ||||
add(swhId); | add(swhPID); | ||||
} | } | ||||
} | } | ||||
/** | /** | ||||
* Returns this path as a list of {@link SwhId}. | * Returns this path as a list of {@link SwhPID}. | ||||
* | * | ||||
* @return list of {@link SwhId} constituting the path | * @return list of {@link SwhPID} constituting the path | ||||
* @see org.softwareheritage.graph.SwhId | * @see org.softwareheritage.graph.SwhPID | ||||
*/ | */ | ||||
@JsonValue | @JsonValue | ||||
public ArrayList<SwhId> getPath() { | public ArrayList<SwhPID> getPath() { | ||||
return path; | return path; | ||||
} | } | ||||
/** | /** | ||||
* Adds a {@link SwhId} to this path. | * Adds a {@link SwhPID} to this path. | ||||
* | * | ||||
* @param {@link SwhId} to add to this path | * @param {@link SwhPID} to add to this path | ||||
* @see org.softwareheritage.graph.SwhId | * @see org.softwareheritage.graph.SwhPID | ||||
*/ | */ | ||||
public void add(SwhId swhId) { | public void add(SwhPID swhPID) { | ||||
path.add(swhId); | path.add(swhPID); | ||||
} | } | ||||
/** | /** | ||||
* Returns the {@link SwhId} at the specified position in this path. | * Returns the {@link SwhPID} at the specified position in this path. | ||||
* | * | ||||
* @param index position of the {@link SwhId} to return | * @param index position of the {@link SwhPID} to return | ||||
* @return {@link SwhId} at the specified position | * @return {@link SwhPID} at the specified position | ||||
* @see org.softwareheritage.graph.SwhId | * @see org.softwareheritage.graph.SwhPID | ||||
*/ | */ | ||||
public SwhId get(int index) { | public SwhPID get(int index) { | ||||
return path.get(index); | return path.get(index); | ||||
} | } | ||||
/** | /** | ||||
* Returns the number of elements in this path. | * Returns the number of elements in this path. | ||||
* | * | ||||
* @return number of elements in this path | * @return number of elements in this path | ||||
*/ | */ | ||||
public int size() { | public int size() { | ||||
return path.size(); | return path.size(); | ||||
} | } | ||||
@Override | @Override | ||||
public boolean equals(Object otherObj) { | public boolean equals(Object otherObj) { | ||||
if (otherObj == this) return true; | if (otherObj == this) return true; | ||||
if (!(otherObj instanceof SwhPath)) return false; | if (!(otherObj instanceof SwhPath)) return false; | ||||
SwhPath other = (SwhPath) otherObj; | SwhPath other = (SwhPath) otherObj; | ||||
if (size() != other.size()) { | if (size() != other.size()) { | ||||
return false; | return false; | ||||
} | } | ||||
for (int i = 0; i < size(); i++) { | for (int i = 0; i < size(); i++) { | ||||
SwhId thisSwhId = get(i); | SwhPID thisSwhPID = get(i); | ||||
SwhId otherSwhId = other.get(i); | SwhPID otherSwhPID = other.get(i); | ||||
if (!thisSwhId.equals(otherSwhId)) { | if (!thisSwhPID.equals(otherSwhPID)) { | ||||
return false; | return false; | ||||
} | } | ||||
} | } | ||||
return true; | return true; | ||||
} | } | ||||
@Override | @Override | ||||
public String toString() { | public String toString() { | ||||
String str = new String(); | String str = new String(); | ||||
for (SwhId swhId : path) { | for (SwhPID swhPID : path) { | ||||
str += swhId + "/"; | str += swhPID + "/"; | ||||
} | } | ||||
return str; | return str; | ||||
} | } | ||||
} | } |