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.UUID;
022
023/**
024 * A repository backed by an IDE workspace, the output of a build session or similar ad-hoc collection of artifacts. As
025 * far as the repository system is concerned, a workspace repository is read-only, i.e. can only be used for artifact
026 * resolution but not installation/deployment. Note that this class merely describes such a repository, actual access to
027 * the contained artifacts is handled by a {@link WorkspaceReader}.
028 */
029public final class WorkspaceRepository implements ArtifactRepository {
030
031    private final String type;
032
033    private final Object key;
034
035    /**
036     * Creates a new workspace repository of type {@code "workspace"} and a random key.
037     */
038    public WorkspaceRepository() {
039        this("workspace");
040    }
041
042    /**
043     * Creates a new workspace repository with the specified type and a random key.
044     *
045     * @param type The type of the repository, may be {@code null}.
046     */
047    public WorkspaceRepository(String type) {
048        this(type, null);
049    }
050
051    /**
052     * Creates a new workspace repository with the specified type and key. The key is used to distinguish one workspace
053     * from another and should be sensitive to the artifacts that are (potentially) available in the workspace.
054     *
055     * @param type The type of the repository, may be {@code null}.
056     * @param key The (comparison) key for the repository, may be {@code null} to generate a unique random key.
057     */
058    public WorkspaceRepository(String type, Object key) {
059        this.type = (type != null) ? type : "";
060        this.key = (key != null) ? key : UUID.randomUUID().toString().replace("-", "");
061    }
062
063    public String getContentType() {
064        return type;
065    }
066
067    public String getId() {
068        return "workspace";
069    }
070
071    /**
072     * Gets the key of this workspace repository. The key is used to distinguish one workspace from another and should
073     * be sensitive to the artifacts that are (potentially) available in the workspace.
074     *
075     * @return The (comparison) key for this workspace repository, never {@code null}.
076     */
077    public Object getKey() {
078        return key;
079    }
080
081    @Override
082    public String toString() {
083        return "(" + getContentType() + ")";
084    }
085
086    @Override
087    public boolean equals(Object obj) {
088        if (this == obj) {
089            return true;
090        }
091        if (obj == null || !getClass().equals(obj.getClass())) {
092            return false;
093        }
094
095        WorkspaceRepository that = (WorkspaceRepository) obj;
096
097        return getContentType().equals(that.getContentType()) && getKey().equals(that.getKey());
098    }
099
100    @Override
101    public int hashCode() {
102        int hash = 17;
103        hash = hash * 31 + getKey().hashCode();
104        hash = hash * 31 + getContentType().hashCode();
105        return hash;
106    }
107}