Skip to content

Commit

Permalink
Add utility methods to cast resource to its particular type (eclipse-…
Browse files Browse the repository at this point in the history
  • Loading branch information
Vladyslav Zhukovskyi committed Jan 11, 2017
1 parent 72a168b commit 83c5908
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 0 deletions.
Expand Up @@ -95,6 +95,28 @@ public interface Resource extends Comparable<Resource> {
*/
boolean isFile();

/**
* Casts current resource to the {@link File} if the last one's represents a file.
* <p>
* Example of usage:
* <pre>
* public void doSome() {
* Resource resource = ...;
* if (resource.isFile()) {
* File file = resource.asFile();
* }
* }
* </pre>
*
* @return instance of {@link File}
* @throws IllegalStateException
* in case if current resource is not a file
* @see Resource#getResourceType()
* @see Resource#FILE
* @since 5.1.0
*/
File asFile();

/**
* Returns {@code true} if current represents a folder.
*
Expand All @@ -105,6 +127,28 @@ public interface Resource extends Comparable<Resource> {
*/
boolean isFolder();

/**
* Casts current resource to the {@link Folder} if the last one's represents a folder.
* <p>
* Example of usage:
* <pre>
* public void doSome() {
* Resource resource = ...;
* if (resource.isFolder()) {
* Folder folder = resource.asFolder();
* }
* }
* </pre>
*
* @return instance of {@link Folder}
* @throws IllegalStateException
* in case if current resource is not a folder
* @see Resource#getResourceType()
* @see Resource#FOLDER
* @since 5.1.0
*/
Folder asFolder();

/**
* Returns {@code true} if current represents a project.
*
Expand All @@ -115,6 +159,28 @@ public interface Resource extends Comparable<Resource> {
*/
boolean isProject();

/**
* Casts current resource to the {@link Project} if the last one's represents a project.
* <p>
* Example of usage:
* <pre>
* public void doSome() {
* Resource resource = ...;
* if (resource.isProject()) {
* Project project = resource.asProject();
* }
* }
* </pre>
*
* @return instance of {@link Project}
* @throws IllegalStateException
* in case if current resource is not a project
* @see Resource#getResourceType()
* @see Resource#PROJECT
* @since 5.1.0
*/
Project asProject();

/**
* Copies resource to given {@code destination} path. Copy operation performs asynchronously and result of current
* operation will be provided in {@code Promise} result. Destination path should have write access.
Expand Down
Expand Up @@ -17,6 +17,8 @@

import org.eclipse.che.api.promises.client.Promise;
import org.eclipse.che.ide.api.resources.Container;
import org.eclipse.che.ide.api.resources.File;
import org.eclipse.che.ide.api.resources.Folder;
import org.eclipse.che.ide.api.resources.Project;
import org.eclipse.che.ide.api.resources.Resource;
import org.eclipse.che.ide.api.resources.marker.Marker;
Expand All @@ -27,6 +29,7 @@
import static com.google.common.base.Optional.of;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState;
import static com.google.common.base.Strings.isNullOrEmpty;
import static java.lang.System.arraycopy;
import static java.util.Arrays.copyOf;
Expand Down Expand Up @@ -60,18 +63,39 @@ public boolean isFile() {
return getResourceType() == FILE;
}

@Override
public File asFile() {
checkState(isFile(), "Current resource is not a file");

return (File)this;
}

/** {@inheritDoc} */
@Override
public boolean isFolder() {
return getResourceType() == FOLDER;
}

@Override
public Folder asFolder() {
checkState(isFolder(), "Current resource is not a folder");

return (Folder)this;
}

/** {@inheritDoc} */
@Override
public boolean isProject() {
return getResourceType() == PROJECT;
}

@Override
public Project asProject() {
checkState(isProject(), "Current resource is not a project");

return (Project)this;
}

/** {@inheritDoc} */
@Override
public Promise<Resource> copy(Path destination) {
Expand Down

0 comments on commit 83c5908

Please sign in to comment.