001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.eclipse.aether.repository;
020
021import java.util.Objects;
022import java.util.UUID;
023
024/**
025 * A repository backed by an IDE workspace, the output of a build session or similar ad-hoc collection of artifacts. As
026 * far as the repository system is concerned, a workspace repository is read-only, i.e. can only be used for artifact
027 * resolution but not installation/deployment. Note that this class merely describes such a repository, actual access to
028 * the contained artifacts is handled by a {@link WorkspaceReader}.
029 */
030public final class WorkspaceRepository implements ArtifactRepository {
031    /**
032     * The identifier of this workspace repository type, returned by {@link #getId()}.
033     */
034    public static final String ID = "workspace";
035
036    private final String type;
037
038    private final Object key;
039
040    private final int hashCode;
041
042    /**
043     * Creates a new workspace repository of type {@code "workspace"} and a random key.
044     */
045    public WorkspaceRepository() {
046        this("workspace");
047    }
048
049    /**
050     * Creates a new workspace repository with the specified type and a random key.
051     *
052     * @param type The type of the repository, may be {@code null}.
053     */
054    public WorkspaceRepository(String type) {
055        this(type, null);
056    }
057
058    /**
059     * Creates a new workspace repository with the specified type and key. The key is used to distinguish one workspace
060     * from another and should be sensitive to the artifacts that are (potentially) available in the workspace.
061     *
062     * @param type The type of the repository, may be {@code null}.
063     * @param key The (comparison) key for the repository, may be {@code null} to generate a unique random key.
064     */
065    public WorkspaceRepository(String type, Object key) {
066        this.type = (type != null) ? type : "";
067        this.key = (key != null) ? key : UUID.randomUUID().toString().replace("-", "");
068        this.hashCode = Objects.hash(type, key);
069    }
070
071    public String getContentType() {
072        return type;
073    }
074
075    public String getId() {
076        return ID;
077    }
078
079    /**
080     * Gets the key of this workspace repository. The key is used to distinguish one workspace from another and should
081     * be sensitive to the artifacts that are (potentially) available in the workspace.
082     *
083     * @return The (comparison) key for this workspace repository, never {@code null}.
084     */
085    public Object getKey() {
086        return key;
087    }
088
089    @Override
090    public String toString() {
091        return "(" + getContentType() + ")";
092    }
093
094    @Override
095    public boolean equals(Object obj) {
096        if (this == obj) {
097            return true;
098        }
099        if (obj == null || !getClass().equals(obj.getClass())) {
100            return false;
101        }
102
103        WorkspaceRepository that = (WorkspaceRepository) obj;
104
105        return getContentType().equals(that.getContentType()) && getKey().equals(that.getKey());
106    }
107
108    @Override
109    public int hashCode() {
110        return hashCode;
111    }
112}