001// Licensed under the Apache License, Version 2.0 (the "License");
002// you may not use this file except in compliance with the License.
003// You may obtain a copy of the License at
004//
005// http://www.apache.org/licenses/LICENSE-2.0
006//
007// Unless required by applicable law or agreed to in writing, software
008// distributed under the License is distributed on an "AS IS" BASIS,
009// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
010// See the License for the specific language governing permissions and
011// limitations under the License.
012package org.apache.tapestry5.versionmigrator;
013
014/**
015 * Class that represents information about one class being renamed and/or moved
016 * between artifacts (JARs) and/or packages.
017 */
018final public class ClassRefactor 
019{
020    
021    final private String newClassName;
022    final private String oldClassName;
023    final private String sourceArtifact;
024    final private String destinationArtifact;
025    
026    /**
027     * Constructor for classes being moved from one artifact to another
028     * and possibly being renamed or moved between packages.
029     */
030    public ClassRefactor(String newClassName, String oldClassName, String sourceArtifact, String destinationArtifact) 
031    {
032        super();
033        verifyNotBlank("newClassName", newClassName);
034        verifyNotBlank("oldClassName", oldClassName);
035        verifyNotBlank("sourceArtifact", sourceArtifact);
036        verifyNotBlank("destinationArtifact", destinationArtifact);
037        this.newClassName = newClassName;
038        this.oldClassName = oldClassName;
039        this.sourceArtifact = sourceArtifact;
040        this.destinationArtifact = destinationArtifact;
041    }
042    
043    /**
044     * Returns the new fully-qualified class name.
045     */
046    public String getNewClassName() 
047    {
048        return newClassName;
049    }
050    
051    /**
052     * Returns the old fully-qualified class name.
053     */
054    public String getOldClassName() 
055    {
056        return oldClassName;
057    }
058    
059    /**
060     * Returns the artifact where the class was located.
061     */
062    public String getSourceArtifact() 
063    {
064        return sourceArtifact;
065    }
066    
067    /**
068     * Returns the artifact where the class is now located.
069     */
070    public String getDestinationArtifact() 
071    {
072        return destinationArtifact;
073    }
074    
075    /**
076     * Returns whether the class was moved between artifacts.
077     */
078    public boolean isMovedBetweenArtifacts() 
079    {
080        return !sourceArtifact.equals(destinationArtifact);
081    }
082
083    /**
084     * Returns whether the class had its fully qualified class name changed.
085     * This includes package changes.
086     */
087    public boolean isRenamed() 
088    {
089        return !oldClassName.equals(newClassName);
090    }
091
092    @Override
093    public String toString() {
094        return "ClassMoveInformation [newClassName=" + newClassName + ", oldClassName=" + oldClassName + ", sourceArtifact=" + sourceArtifact
095                + ", destinationArtifact=" + destinationArtifact + "]";
096    }
097 
098    final static boolean isNotBlank(String string)
099    {
100        return string != null && string.trim().length() > 0;
101    }
102    
103    final static void verifyNotBlank(String parameterName, String parameterValue)
104    {
105        if (!isNotBlank(parameterValue))
106        {
107            throw new IllegalArgumentException(
108                    String.format("Parameter %s cannot be null nor blank", parameterName));
109        }
110    }
111 
112    /**
113     * Returns the simple old class name.
114     */
115    public String getSimpleOldClassName() {
116        return oldClassName.substring(oldClassName.lastIndexOf(".") + 1);
117    }
118    
119    /**
120     * Returns whether the class is internal or not.
121     */
122    public boolean isInternal()
123    {
124        return oldClassName.contains(".internal.");
125    }
126    
127    /**
128     * Returns the new package location.
129     */
130    public String getNewPackageName() 
131    {
132        return newClassName.substring(0, newClassName.lastIndexOf("."));
133    }
134
135}