Skip to content

Commit

Permalink
Resource API Improvements - Just a few encapsulated fields. Renamed 1…
Browse files Browse the repository at this point in the history
… unused method.
  • Loading branch information
lincolnthree committed Jan 17, 2014
1 parent 3afbe9a commit 3b49980
Show file tree
Hide file tree
Showing 14 changed files with 103 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ private void listRepositories(List<Resource<?>> children)
List<Repository> repositories = getCurrentModel().getRepositories();
for (Repository repository : repositories)
{
children.add(new MavenRepositoryResourceImpl(getResourceFactory(), parent, repository));
children.add(new MavenRepositoryResourceImpl(getResourceFactory(), getParent(), repository));
}
}

Expand Down Expand Up @@ -100,7 +100,7 @@ public Model getCurrentModel()
@Override
public Resource<File> createFrom(File file)
{
return new MavenPomResourceImpl(resourceFactory, file);
return new MavenPomResourceImpl(getResourceFactory(), file);
}

private void initialize()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class JavaProjectTypeStep extends AbstractUICommand implements UIWizardSt
{

@Inject
@WithAttributes(label = "Create Main Class?", description = "Toggle creation of a simple Main() script in the root package, valid for jar projects only")
@WithAttributes(label = "Create Main Class", description = "Toggle creation of a simple Main() script in the root package, valid for jar projects only")
private UIInput<Boolean> createMain;

@Override
Expand All @@ -53,6 +53,7 @@ public UICommandMetadata getMetadata(UIContext context)
.description("Information setup for the Java project type");
}

@Override
public void initializeUI(UIBuilder builder) throws Exception
{
builder.add(createMain);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public JavaResourceImpl(final ResourceFactory factory, JavaSourceFactory parser,
public Resource<?> getChild(final String name)
{
List<Resource<?>> children = doListResources();
List<Resource<?>> subset = new ArrayList<Resource<?>>();
List<Resource<?>> subset = new ArrayList<>();

for (Resource<?> child : children)
{
Expand Down Expand Up @@ -81,7 +81,7 @@ protected synchronized List<Resource<?>> doListResources()
{
try
{
List<Resource<?>> list = new LinkedList<Resource<?>>();
List<Resource<?>> list = new LinkedList<>();

for (Member<?, ?> member : getJavaSource().getMembers())
{
Expand Down Expand Up @@ -138,7 +138,7 @@ public JavaSource<?> getJavaSource() throws FileNotFoundException
@Override
public JavaResourceImpl createFrom(final File file)
{
return new JavaResourceImpl(resourceFactory, parser, file);
return new JavaResourceImpl(getResourceFactory(), parser, file);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public Node getXmlSource() throws FileNotFoundException
@Override
public Resource<File> createFrom(File file)
{
return new XMLResourceImpl(resourceFactory, file);
return new XMLResourceImpl(getResourceFactory(), file);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public InputStream getResourceInputStream()
@Override
public DirectoryResource getParent()
{
return file.getParentFile() != null ? resourceFactory.create(DirectoryResource.class, file.getParentFile())
return file.getParentFile() != null ? getResourceFactory().create(DirectoryResource.class, file.getParentFile())
: null;
}

Expand Down Expand Up @@ -107,7 +107,7 @@ public boolean isStale()
}

@Override
public void markUpToDate()
public void refresh()
{
lastModification = getUnderlyingResourceObject().lastModified();
}
Expand Down Expand Up @@ -360,18 +360,18 @@ public String getFullyQualifiedName()
@Override
public ResourceMonitor monitor()
{
return resourceFactory.monitor(this);
return getResourceFactory().monitor(this);
}

@Override
public ResourceMonitor monitor(ResourceFilter filter)
{
return resourceFactory.monitor(this, filter);
return getResourceFactory().monitor(this, filter);
}

protected FileOperations getFileOperations()
{
return resourceFactory.getFileOperations();
return getResourceFactory().getFileOperations();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@

/**
* @author Mike Brock <cbrock@redhat.com>
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
* @author <a href="http://community.jboss.org/people/kenfinni">Ken Finnigan</a>
*/
public abstract class AbstractResource<T> extends AbstractFaceted<ResourceFacet> implements Resource<T>
{
protected final ResourceFactory resourceFactory;
protected Resource<?> parent;
private final ResourceFactory resourceFactory;
private Resource<?> parent;

protected AbstractResource(final ResourceFactory factory, final Resource<?> parent)
{
Expand Down Expand Up @@ -52,6 +53,14 @@ public Resource<?> getParent()
return parent;
}

/**
* Set the parent {@link Resource}.
*/
protected void setParent(Resource<?> parent)
{
this.parent = parent;
}

@Override
public <R extends Resource<?>> R reify(final Class<R> type)
{
Expand Down Expand Up @@ -84,7 +93,7 @@ public synchronized List<Resource<?>> listResources()
@Override
public synchronized List<Resource<?>> listResources(final ResourceFilter filter)
{
List<Resource<?>> result = new ArrayList<Resource<?>>();
List<Resource<?>> result = new ArrayList<>();
for (Resource<?> resource : doListResources())
{
if (filter.accept(resource))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
/**
* A standard, built-in resource for representing files on the filesystem.
*
* @author Mike Brock
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
*/
public interface FileResource<T extends FileResource<T>> extends Resource<File>
Expand All @@ -27,9 +26,9 @@ public interface FileResource<T extends FileResource<T>> extends Resource<File>
public boolean isStale();

/**
* Re-read the last modified timestamp for this resource.
* Re-read the file-system meta-data for this resource (such as last modified time-stamp, and permissions.)
*/
public void markUpToDate();
public void refresh();

/**
* Create a new single directory for this resource. This will not succeed if any parent directories needed for this
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright 2012 Red Hat, Inc. and/or its affiliates.
*
* Licensed under the Eclipse Public License version 1.0, available at
* http://www.eclipse.org/legal/epl-v10.html
*/

package org.jboss.forge.addon.resource.events;

import org.jboss.forge.addon.resource.Resource;

/**
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
*/
public class ResourceRenamed extends ResourceModified
{
private final String originalLocation;

public ResourceRenamed(final Resource<?> resource, final String originalLocation)
{
super(resource);
this.originalLocation = originalLocation;
}

public String getOriginalLocation()
{
return originalLocation;
}

@Override
public int hashCode()
{
final int prime = 31;
int result = super.hashCode();
result = prime * result + ((originalLocation == null) ? 0 : originalLocation.hashCode());
return result;
}

@Override
public boolean equals(Object obj)
{
if (this == obj)
return true;
if (!super.equals(obj))
return false;
if (getClass() != obj.getClass())
return false;
ResourceRenamed other = (ResourceRenamed) obj;
if (originalLocation == null)
{
if (other.originalLocation != null)
return false;
}
else if (!originalLocation.equals(other.originalLocation))
return false;
return true;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
/**
* A standard, build-in, resource for representing directories on the file-system.
*
* @author Mike Brock
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
*/
public class DirectoryResourceImpl extends AbstractFileResource<DirectoryResource> implements DirectoryResource
Expand All @@ -41,14 +40,14 @@ protected List<Resource<?>> doListResources()

if (listCache == null)
{
listCache = new LinkedList<Resource<?>>();
listCache = new LinkedList<>();

File[] files = getFileOperations().listFiles(file);
if (files != null)
{
for (File f : files)
{
listCache.add(resourceFactory.create(f));
listCache.add(getResourceFactory().create(f));
}
}
}
Expand All @@ -62,7 +61,7 @@ protected List<Resource<?>> doListResources()
@Override
public Resource<?> getChild(final String name)
{
return resourceFactory.create(new File(file.getAbsolutePath(), name));
return getResourceFactory().create(new File(file.getAbsolutePath(), name));
}

/**
Expand All @@ -84,7 +83,7 @@ public DirectoryResourceImpl getChildDirectory(final String name) throws Resourc

if (!(result instanceof DirectoryResourceImpl))
{
result = new DirectoryResourceImpl(resourceFactory, new File(file.getAbsoluteFile(), name));
result = new DirectoryResourceImpl(getResourceFactory(), new File(file.getAbsoluteFile(), name));
}
return (DirectoryResourceImpl) result;
}
Expand Down Expand Up @@ -123,7 +122,7 @@ else if (child.exists())
else
{
E underlyingResource = (E) child.getUnderlyingResourceObject();
result = resourceFactory.create(type, underlyingResource);
result = getResourceFactory().create(type, underlyingResource);
}
return result;
}
Expand Down Expand Up @@ -155,23 +154,23 @@ else if (!getFileOperations().fileExists(file))
getFileOperations().mkdirs(file);
}

return new DirectoryResourceImpl(resourceFactory, file);
return new DirectoryResourceImpl(getResourceFactory(), file);
}

@Override
public synchronized DirectoryResource getParent()
{
if (parent == null)
if (getParent() == null)
{
File parentFile = file.getParentFile();
if (parentFile == null)
{
return null;
}

parent = createFrom(parentFile);
setParent(createFrom(parentFile));
}
return (DirectoryResource) parent;
return getParent();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public FileResourceImpl(final ResourceFactory factory, final File file)
@Override
public FileResourceImpl createFrom(final File file)
{
return new FileResourceImpl(resourceFactory, file);
return new FileResourceImpl(getResourceFactory(), file);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public <E, T extends Resource<E>> T create(final Class<T> type, final E underlyi
T result = null;
synchronized (this)
{
TreeMap<Class<?>, ResourceGenerator> generated = new TreeMap<Class<?>, ResourceGenerator>(
TreeMap<Class<?>, ResourceGenerator> generated = new TreeMap<>(
new RelatedClassComparator());

// FIXME Workaround for FORGE-1263
Expand Down Expand Up @@ -121,5 +121,4 @@ public FileOperations getFileOperations()
return DefaultFileOperations.INSTANCE;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public boolean isStarted()
public Set<ResourceEvent> getChangeSet()
{
assertSessionCreated();
Set<ResourceEvent> changes = new LinkedHashSet<ResourceEvent>();
Set<ResourceEvent> changes = new LinkedHashSet<>();
try
{
// Using reflection, since the field is unavailable
Expand Down Expand Up @@ -336,7 +336,7 @@ public boolean mkdirs(File file)
try
{
// Must create the whole structure
LinkedList<File> stack = new LinkedList<File>();
LinkedList<File> stack = new LinkedList<>();
File parent = file;
while (parent != null && !fileExistsAndIsDirectory(parent))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public Shell createShell(File initialSelection, Settings settings)
{
Assert.notNull(settings, "Settings cannot be null");
FileResource<?> initialResource = resourceFactory.create(initialSelection).reify(FileResource.class);
return new ShellImpl(initialResource, settings, commandManager, addonRegistry, commandFactory);
return new ShellImpl(resourceFactory, initialResource, settings, commandManager, addonRegistry, commandFactory);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.jboss.forge.addon.resource.DirectoryResource;
import org.jboss.forge.addon.resource.FileResource;
import org.jboss.forge.addon.resource.Resource;
import org.jboss.forge.addon.resource.ResourceFactory;
import org.jboss.forge.addon.shell.aesh.ForgeCommandRegistry;
import org.jboss.forge.addon.shell.aesh.ForgeManProvider;
import org.jboss.forge.addon.shell.ui.ShellContextImpl;
Expand Down Expand Up @@ -63,8 +64,8 @@ public class ShellImpl implements Shell, UIRuntime
private final UIOutput output;
private final List<CommandExecutionListener> executionListeners = new LinkedList<>();

public ShellImpl(FileResource<?> initialResource, Settings settings, CommandManager commandManager,
AddonRegistry addonRegistry, CommandControllerFactory commandFactory)
public ShellImpl(ResourceFactory factory, FileResource<?> initialResource, Settings settings,
CommandManager commandManager, AddonRegistry addonRegistry, CommandControllerFactory commandFactory)
{
this.currentResource = initialResource;
this.addonRegistry = addonRegistry;
Expand Down

0 comments on commit 3b49980

Please sign in to comment.