Class ArtifactModel


  • public class ArtifactModel
    extends java.lang.Object
    Implements the artifact model starting at a certain file root. However, the artifact model may also contain related artifacts outside the base directory subtree which must then be absolute. Typically, an artifact model will be created up incrementally but may be created in one step using scanAll(ProgressObserver).
    Author:
    Holger Eichelberger
    • Constructor Summary

      Constructors 
      Constructor Description
      ArtifactModel​(java.io.File base)
      Creates an artifact model instance as instantiation environment.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      (package private) void afterRename​(IFileSystemArtifact artifact)
      Called after rename in order to create the changed artifacts.
      (package private) void afterRename​(Path path)
      Called after a path was renamed.
      (package private) void beforeRename​(IFileSystemArtifact artifact)
      Called before renaming an artifact to cleanup.
      (package private) void beforeRename​(Path path)
      Called before a path is renamed, i.e., it is about to be renamed.
      (package private) void clear()
      Clears this artifact.
      (package private) void delete​(java.io.File path)
      Deletes a path and possible related artifacts.
      (package private) void delete​(Path path)
      Deletes a path and possible related artifacts.
      (package private) void discardPath​(Path path)
      Discards a path before it becomes invalid, e.g., as its scope is discarded during execution.
      (package private) IArtifact getArtifact​(java.lang.Object real)
      Returns a known artifact.
      java.io.File getBase()
      Returns the base directory.
      java.lang.String getBasePath()
      Returns the (absolute) base path of getBase().
      (package private) Path getPathFromCache​(java.lang.String pathKey)
      Returns a path from the path cache.
      private java.lang.String getPathKey​(Path path)
      Returns the internal path cache key for path.
      java.lang.Object getSettings​(ProjectSettings key)
      Returns the settings object for the specified key.
      (package private) int handles​(java.lang.Object real)
      Returns whether this artifact model handles the given real object.
      private static boolean isOfType​(java.lang.Class<?> type, IFileSystemArtifact artifact, boolean byKind, boolean negate)
      Returns whether type selection matches.
      (package private) java.lang.String makeRelative​(java.io.File file)
      Makes the given file relative to this model.
      (package private) static java.lang.String makeRelative​(java.io.File file, ArtifactModel model)
      Makes the given file relative to the given model.
      (package private) java.lang.String makeRelative​(java.lang.String path)
      Makes the given path relative to this model.
      (package private) static java.lang.String makeRelative​(java.lang.String path, ArtifactModel model)
      Makes the given path relative to the given model.
      (package private) java.lang.String makeRelativeFile​(java.io.File file)
      Makes the given file relative to this model.
      private static boolean matchesPath​(Path path, IFileSystemArtifact artifact)
      Returns whether the given path (may be a pattern) matches the path of artifact.
      (package private) boolean register​(java.lang.Object real, IArtifact artifact)
      Registers a given artifact created by the artifact factory.
      (package private) void registerPath​(Path path)
      Called if a path is created.
      private int scanAll​(java.io.File location, int count, ProgressObserver observer, ProgressObserver.ITask task, java.util.List<VilException> errors)
      Processes all files in location in a recursive manner.
      void scanAll​(ProgressObserver observer)
      Scans the entire environment starting at root.
      Set<FolderArtifact> selectAllFolders()
      Returns all artifacts.
      Set<FileArtifact> selectByKind​(java.lang.Class<?> type, boolean negate)
      Does type selection of artifacts by subtyping.
      Set<FileArtifact> selectByName​(java.lang.String name)
      Does name selection of artifacts.
      (package private) void selectByName​(java.util.regex.Pattern pattern, java.lang.String name, java.util.List<FileArtifact> result)
      Does type selection of artifacts based on path and type.
      Set<FileArtifact> selectByType​(java.lang.Class<?> type, boolean negate)
      Does type selection of artifacts by type equality.
      (package private) void selectByType​(Path path, java.lang.Class<?> type, java.util.List<FileArtifact> result, boolean byKind, boolean negate)
      Does type selection of artifacts based on path and type.
      void setSettings​(ProjectSettings key, java.lang.Object object)
      Sets the settings for the artifact model.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • base

        private java.io.File base
      • basePath

        private java.lang.String basePath
      • fileArtifacts

        private java.util.TreeMap<java.lang.String,​IFileSystemArtifact> fileArtifacts
      • otherArtifacts

        private java.util.TreeMap<java.lang.Object,​IArtifact> otherArtifacts
      • settings

        private java.util.Map<ProjectSettings,​java.lang.Object> settings
      • pathCache

        private java.util.Map<java.lang.String,​Path> pathCache
    • Constructor Detail

      • ArtifactModel

        ArtifactModel​(java.io.File base)
        Creates an artifact model instance as instantiation environment.
        Parameters:
        base - the base directory of this artifact model (may be null but only as default model for the factory)
    • Method Detail

      • getBase

        public java.io.File getBase()
        Returns the base directory.
        Returns:
        the base directory
      • getBasePath

        public java.lang.String getBasePath()
        Returns the (absolute) base path of getBase().
        Returns:
        the absolute base path
      • scanAll

        public void scanAll​(ProgressObserver observer)
                     throws VilException
        Scans the entire environment starting at root.
        Parameters:
        observer - the observer to be notified in case of progress, use ProgressObserver.NO_OBSERVER if progress shall not be monitored
        Throws:
        VilException - in case that creating / obtaining artifacts fails
      • scanAll

        private int scanAll​(java.io.File location,
                            int count,
                            ProgressObserver observer,
                            ProgressObserver.ITask task,
                            java.util.List<VilException> errors)
        Processes all files in location in a recursive manner. May be used to count the number of files to be processed.
        Parameters:
        location - the location to be scanned
        count - the number of files counted so far (call initially with 0)
        observer - the progress observer (do not create artifacts if this is null)
        task - the progress task (ignored if null)
        errors - exceptions caught during scanning (do not create artifacts if this is null)
        Returns:
        the number of files processed
        See Also:
        ArtifactFactory.createFileSystemArtifact(File)
      • selectAllFolders

        public Set<FolderArtifact> selectAllFolders()
        Returns all artifacts.
        Returns:
        the selected artifacts
      • selectByType

        public Set<FileArtifact> selectByType​(java.lang.Class<?> type,
                                              boolean negate)
        Does type selection of artifacts by type equality.
        Parameters:
        type - the target type
        negate - negate the selection
        Returns:
        the selected artifacts (the type will be adjusted to the actual type of type)
      • selectByKind

        public Set<FileArtifact> selectByKind​(java.lang.Class<?> type,
                                              boolean negate)
        Does type selection of artifacts by subtyping.
        Parameters:
        type - the target type
        negate - negate the selection
        Returns:
        the selected artifacts (the type will be adjusted to the actual type of type)
      • selectByName

        public Set<FileArtifact> selectByName​(java.lang.String name)
        Does name selection of artifacts.
        Parameters:
        name - the regular name pattern for artifact selection
        Returns:
        the selected artifacts
      • selectByName

        void selectByName​(java.util.regex.Pattern pattern,
                          java.lang.String name,
                          java.util.List<FileArtifact> result)
        Does type selection of artifacts based on path and type.
        Parameters:
        pattern - an optional regular expression to be matched (may be null)
        name - the name used to build the pattern used as fallback if pattern is null
        result - to be modified as a side effect
      • selectByType

        void selectByType​(Path path,
                          java.lang.Class<?> type,
                          java.util.List<FileArtifact> result,
                          boolean byKind,
                          boolean negate)
        Does type selection of artifacts based on path and type.
        Parameters:
        path - the path to be considered for selection (may be null)
        type - the type to be considered (may be null, than this parameter is ignored)
        result - to be modified as a side effect
        byKind - do the selection by subtyping (true) or by equality (false)
        negate - negate the selection
      • isOfType

        private static boolean isOfType​(java.lang.Class<?> type,
                                        IFileSystemArtifact artifact,
                                        boolean byKind,
                                        boolean negate)
        Returns whether type selection matches.
        Parameters:
        type - the type to be considered (may be null, than this parameter is ignored)
        artifact - the artifact to test
        byKind - do the selection by subtyping (true) or by equality (false)
        negate - negate the result
        Returns:
        true if types match, false else
      • matchesPath

        private static boolean matchesPath​(Path path,
                                           IFileSystemArtifact artifact)
        Returns whether the given path (may be a pattern) matches the path of artifact.
        Parameters:
        path - the path to be considered (as pattern)
        artifact - the artifact to be matched against the pattern
        Returns:
        true if it matches, false else
      • register

        boolean register​(java.lang.Object real,
                         IArtifact artifact)
        Registers a given artifact created by the artifact factory.
        Parameters:
        real - the real underlying artifact
        artifact - the artifact to be registered
        Returns:
        true if the artifact was registered, false else
      • makeRelativeFile

        java.lang.String makeRelativeFile​(java.io.File file)
        Makes the given file relative to this model.
        Parameters:
        file - the file to make relative
        Returns:
        the normalized and relative file if this model matches as prefix, the normalized absolute file path else
      • makeRelative

        java.lang.String makeRelative​(java.io.File file)
        Makes the given file relative to this model.
        Parameters:
        file - the file to make relative
        Returns:
        the normalized and relative path if this model matches as prefix, the normalized absolute file else
      • makeRelative

        java.lang.String makeRelative​(java.lang.String path)
        Makes the given path relative to this model.
        Parameters:
        path - the path to make relative
        Returns:
        the normalized and relative path if this model matches as prefix, the normalized absolute file else
      • makeRelative

        static java.lang.String makeRelative​(java.lang.String path,
                                             ArtifactModel model)
        Makes the given path relative to the given model.
        Parameters:
        path - the path to make relative
        model - the model to consider (may be null)
        Returns:
        the normalized and relative path if model is given and matches as prefix, the normalized absolute file else
      • makeRelative

        static java.lang.String makeRelative​(java.io.File file,
                                             ArtifactModel model)
        Makes the given file relative to the given model.
        Parameters:
        file - the file to make relative
        model - the model to consider (may be null)
        Returns:
        the normalized and relative path if model is given and matches as prefix, the normalized absolute file else
      • getArtifact

        IArtifact getArtifact​(java.lang.Object real)
        Returns a known artifact.
        Parameters:
        real - the real underlying artifact
        Returns:
        the artifact or null if no artifact is known for real
      • handles

        int handles​(java.lang.Object real)
        Returns whether this artifact model handles the given real object.
        Parameters:
        real - the real object
        Returns:
        a numeric value allowing to rank multiple artifact models handling real, i.e., 0 if it does not handle, 1 if it basically handles, a larger number for higher priority (e.g., the length of basePath)
      • delete

        void delete​(java.io.File path)
        Deletes a path and possible related artifacts.
        Parameters:
        path - the path being deleted
      • getPathKey

        private java.lang.String getPathKey​(Path path)
        Returns the internal path cache key for path.
        Parameters:
        path - the path
        Returns:
        the path cache key
      • discardPath

        void discardPath​(Path path)
        Discards a path before it becomes invalid, e.g., as its scope is discarded during execution.
        Parameters:
        path - the path to discard
      • getPathFromCache

        Path getPathFromCache​(java.lang.String pathKey)
        Returns a path from the path cache.
        Parameters:
        pathKey - the path key (normalized path)
        Returns:
        the path (may be null if unknown)
      • registerPath

        void registerPath​(Path path)
        Called if a path is created.
        Parameters:
        path - the path to discard
      • beforeRename

        void beforeRename​(IFileSystemArtifact artifact)
                   throws VilException
        Called before renaming an artifact to cleanup.
        Parameters:
        artifact - the artifact being renamed
        Throws:
        VilException - in case of problems obtaining the path
      • afterRename

        void afterRename​(IFileSystemArtifact artifact)
                  throws VilException
        Called after rename in order to create the changed artifacts.
        Parameters:
        artifact - the renamed artifact
        Throws:
        VilException - in case of problems obtaining the path
      • beforeRename

        void beforeRename​(Path path)
        Called before a path is renamed, i.e., it is about to be renamed.
        Parameters:
        path - the path
      • afterRename

        void afterRename​(Path path)
        Called after a path was renamed.
        Parameters:
        path - the path
      • delete

        void delete​(Path path)
        Deletes a path and possible related artifacts. This method is called from Path.
        Parameters:
        path - the path being deleted
      • clear

        void clear()
        Clears this artifact. For internal use only.
      • setSettings

        public void setSettings​(ProjectSettings key,
                                java.lang.Object object)
        Sets the settings for the artifact model.
        Parameters:
        key - ID for the settings object
        object - the settings
      • getSettings

        public java.lang.Object getSettings​(ProjectSettings key)
        Returns the settings object for the specified key.
        Parameters:
        key - the key assigned to the settings object
        Returns:
        the settings object