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    public static final String ID = "workspace";
032
033    private final String type;
034
035    private final Object key;
036
037    private final int hashCode;
038
039    /**
040     * Creates a new workspace repository of type {@code "workspace"} and a random key.
041     */
042    public WorkspaceRepository() {
043        this("workspace");
044    }
045
046    /**
047     * Creates a new workspace repository with the specified type and a random key.
048     *
049     * @param type The type of the repository, may be {@code null}.
050     */
051    public WorkspaceRepository(String type) {
052        this(type, null);
053    }
054
055    /**
056     * Creates a new workspace repository with the specified type and key. The key is used to distinguish one workspace
057     * from another and should be sensitive to the artifacts that are (potentially) available in the workspace.
058     *
059     * @param type The type of the repository, may be {@code null}.
060     * @param key The (comparison) key for the repository, may be {@code null} to generate a unique random key.
061     */
062    public WorkspaceRepository(String type, Object key) {
063        this.type = (type != null) ? type : "";
064        this.key = (key != null) ? key : UUID.randomUUID().toString().replace("-", "");
065        this.hashCode = Objects.hash(type, key);
066    }
067
068    public String getContentType() {
069        return type;
070    }
071
072    public String getId() {
073        return ID;
074    }
075
076    /**
077     * Gets the key of this workspace repository. The key is used to distinguish one workspace from another and should
078     * be sensitive to the artifacts that are (potentially) available in the workspace.
079     *
080     * @return The (comparison) key for this workspace repository, never {@code null}.
081     */
082    public Object getKey() {
083        return key;
084    }
085
086    @Override
087    public String toString() {
088        return "(" + getContentType() + ")";
089    }
090
091    @Override
092    public boolean equals(Object obj) {
093        if (this == obj) {
094            return true;
095        }
096        if (obj == null || !getClass().equals(obj.getClass())) {
097            return false;
098        }
099
100        WorkspaceRepository that = (WorkspaceRepository) obj;
101
102        return getContentType().equals(that.getContentType()) && getKey().equals(that.getKey());
103    }
104
105    @Override
106    public int hashCode() {
107        return hashCode;
108    }
109}