001package org.eclipse.aether.repository;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 * 
012 *  http://www.apache.org/licenses/LICENSE-2.0
013 * 
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
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
031    implements ArtifactRepository
032{
033
034    private final String type;
035
036    private final Object key;
037
038    /**
039     * Creates a new workspace repository of type {@code "workspace"} and a random key.
040     */
041    public WorkspaceRepository()
042    {
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    {
053        this( type, null );
054    }
055
056    /**
057     * Creates a new workspace repository with the specified type and key. The key is used to distinguish one workspace
058     * from another and should be sensitive to the artifacts that are (potentially) available in the workspace.
059     * 
060     * @param type The type of the repository, may be {@code null}.
061     * @param key The (comparison) key for the repository, may be {@code null} to generate a unique random key.
062     */
063    public WorkspaceRepository( String type, Object key )
064    {
065        this.type = ( type != null ) ? type : "";
066        this.key = ( key != null ) ? key : UUID.randomUUID().toString().replace( "-", "" );
067    }
068
069    public String getContentType()
070    {
071        return type;
072    }
073
074    public String getId()
075    {
076        return "workspace";
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    {
087        return key;
088    }
089
090    @Override
091    public String toString()
092    {
093        return "(" + getContentType() + ")";
094    }
095
096    @Override
097    public boolean equals( Object obj )
098    {
099        if ( this == obj )
100        {
101            return true;
102        }
103        if ( obj == null || !getClass().equals( obj.getClass() ) )
104        {
105            return false;
106        }
107
108        WorkspaceRepository that = (WorkspaceRepository) obj;
109
110        return getContentType().equals( that.getContentType() ) && getKey().equals( that.getKey() );
111    }
112
113    @Override
114    public int hashCode()
115    {
116        int hash = 17;
117        hash = hash * 31 + getKey().hashCode();
118        hash = hash * 31 + getContentType().hashCode();
119        return hash;
120    }
121
122}