001package org.apache.maven.scm;
002
003import java.io.Serializable;
004
005/*
006 * Licensed to the Apache Software Foundation (ASF) under one
007 * or more contributor license agreements.  See the NOTICE file
008 * distributed with this work for additional information
009 * regarding copyright ownership.  The ASF licenses this file
010 * to you under the Apache License, Version 2.0 (the
011 * "License"); you may not use this file except in compliance
012 * with the License.  You may obtain a copy of the License at
013 *
014 * http://www.apache.org/licenses/LICENSE-2.0
015 *
016 * Unless required by applicable law or agreed to in writing,
017 * software distributed under the License is distributed on an
018 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
019 * KIND, either express or implied.  See the License for the
020 * specific language governing permissions and limitations
021 * under the License.
022 */
023
024/**
025 * @TODO move to a real enum
026 * <p/>
027 * Typesafe enum for file status
028 * </p>
029 * <p/>
030 * There are two types of status defined in this class: <br/>
031 * 1) Status: Changes in the working tree, not yet committed to the repository eg. MODIFIED <br/>
032 * 2) Transaction: The file is part of some transaction with the repository eg. CHECKED_IN
033 * </p>
034 *
035 * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
036 *
037 */
038public final class ScmFileStatus
039    implements Serializable
040{
041    private static final long serialVersionUID = -7840223279162817915L;
042
043    /**
044     * File is added to the working tree and does not yet exist in the repository
045     */
046    public static final ScmFileStatus ADDED = new ScmFileStatus( "added" );
047
048    /**
049     * File is removed from the working tree thus not revisioned anymore.<br>
050     * The file is still present in the repository.<br>
051     * The file could be deleted from the filesystem depending on the provider.
052     */
053    public static final ScmFileStatus DELETED = new ScmFileStatus( "deleted" );
054
055    /**
056     * The file has been modified in the working tree.
057     */
058    public static final ScmFileStatus MODIFIED = new ScmFileStatus( "modified" );
059
060    /**
061     * The file has been renamed or moved in the working tree.
062     * @since 1.7
063     */
064    public static final ScmFileStatus RENAMED = new ScmFileStatus( "renamed" );
065
066    /**
067     * The file has been copied in the working tree.
068     * @since 1.7
069     */
070    public static final ScmFileStatus COPIED = new ScmFileStatus( "copied" );
071
072    /**
073     * The file is missing in the working tree.
074     */
075    public static final ScmFileStatus MISSING = new ScmFileStatus( "missing" );
076
077    /**
078     * File from working tree is checked into the repository
079     */
080    public static final ScmFileStatus CHECKED_IN = new ScmFileStatus( "checked-in" );
081
082    /**
083     * File is checked out from the repository and into the working tree
084     */
085    public static final ScmFileStatus CHECKED_OUT = new ScmFileStatus( "checked-out" );
086
087    /**
088     * The file in the working tree has differences to the one in repository that
089     * conflicts ie. it cannot automatically be merged.
090     */
091    public static final ScmFileStatus CONFLICT = new ScmFileStatus( "conflict" );
092
093    /**
094     * The file in the working tree has been updated with changes from the repository.
095     */
096    public static final ScmFileStatus PATCHED = new ScmFileStatus( "patched" );
097
098    /**
099     * The file is added, removed or updated from the repository, thus its
100     * up-to-date with the version in the repository. See also isUpdate()
101     */
102    public static final ScmFileStatus UPDATED = new ScmFileStatus( "updated" );
103
104    /**
105     * The file is part of a tag.
106     */
107    public static final ScmFileStatus TAGGED = new ScmFileStatus( "tagged" );
108
109    /**
110     * The file is locked.
111     */
112    public static final ScmFileStatus LOCKED = new ScmFileStatus( "locked" );
113
114    /**
115     * The file is in the working tree but is not versioned and not ignored either.
116     */
117    public static final ScmFileStatus UNKNOWN = new ScmFileStatus( "unknown" );
118
119    /**
120     * @since 1.5
121     * The file is being edited
122     */
123    public static final ScmFileStatus EDITED = new ScmFileStatus( "edit" );
124    
125    /**
126     * The status name
127     */
128    private final String name;
129
130    private ScmFileStatus( String name )
131    {
132        this.name = name;
133    }
134
135    /** {@inheritDoc} */
136    public String toString()
137    {
138        return name;
139    }
140
141    /**
142     * There are changes in the working tree that are not committed to the repository, or <br>
143     * the file is unknown for the working tree.
144     *
145     * @return true on changes in the working tree or if the file is unknown.
146     */
147    public boolean isStatus()
148    {
149        return this == UNKNOWN || isDiff();
150    }
151
152    /**
153     * There are changes in the working tree that are not committed to the repository. <br>
154     *
155     * @return true on changes in the working tree
156     */
157    public boolean isDiff()
158    {
159        return this == ADDED || this == DELETED || this == MODIFIED;
160    }
161
162    /**
163     * @return true if the file was part of a transaction with the repository.
164     */
165    public boolean isTransaction()
166    {
167        return this == CHECKED_IN || this == CHECKED_OUT || this == LOCKED || this == TAGGED || isUpdate();
168    }
169
170    /**
171     * File is part of an update transaction with the repository.<br>
172     * Note: ADDED and REMOVED are not an update status since they indicates
173     * that the working tree has changed.<br>
174     * An update indicates the opposite, that the repository was changed compared to
175     * the working tree and that it is now synchronized unless there are conflicts.
176     *
177     * @return true if the status is conflict, updated or patched.
178     */
179    public boolean isUpdate()
180    {
181        return this == CONFLICT || this == UPDATED || this == PATCHED;
182    }
183}