Skip to content

Commit

Permalink
Add generics to new org.eclipse.core.resource.undo.snapshots
Browse files Browse the repository at this point in the history
  • Loading branch information
mickaelistria committed Sep 21, 2023
1 parent 5e0a997 commit 26702b2
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 100 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
* @since 3.20
*
*/
abstract class AbstractResourceSnapshot implements IResourceSnapshot {
abstract class AbstractResourceSnapshot<T extends IResource> implements IResourceSnapshot<T> {
IContainer parent;

long modificationStamp = IResource.NULL_STAMP;
Expand All @@ -57,7 +57,7 @@ protected AbstractResourceSnapshot() {
*
* @param resource the resource to be described
*/
protected AbstractResourceSnapshot(IResource resource) {
protected AbstractResourceSnapshot(T resource) {
super();
parent = resource.getParent();
if (resource.isAccessible()) {
Expand All @@ -81,9 +81,8 @@ protected AbstractResourceSnapshot(IResource resource) {
}

@Override
public IResource createResource(IProgressMonitor monitor)
throws CoreException {
IResource resource = createResourceHandle();
public T createResource(IProgressMonitor monitor) throws CoreException {
T resource = createResourceHandle();
createExistentResourceFromHandle(resource, monitor);
restoreResourceAttributes(resource);
return resource;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@

package org.eclipse.core.internal.resources.undo.snapshot;

import java.lang.reflect.Array;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
Expand All @@ -40,7 +44,7 @@
*
* @since 3.20
*/
public abstract class ContainerSnapshot extends AbstractResourceSnapshot implements IContainerSnapshot {
public abstract class ContainerSnapshot<T extends IContainer> extends AbstractResourceSnapshot<T> implements IContainerSnapshot<T> {

String name;

Expand All @@ -50,7 +54,7 @@ public abstract class ContainerSnapshot extends AbstractResourceSnapshot impleme

String defaultCharSet;

IResourceSnapshot[] members;
final List<IResourceSnapshot<? extends IResource>> members = new ArrayList<>();

/**
* Create a container snapshot from the specified container handle that can be
Expand All @@ -62,7 +66,7 @@ public abstract class ContainerSnapshot extends AbstractResourceSnapshot impleme
* parents.
*/

public static ContainerSnapshot fromContainer(IContainer container) {
public static <R extends IContainer> ContainerSnapshot<? extends R> fromContainer(R container) {
return fromContainer(container, false);
}

Expand All @@ -76,22 +80,22 @@ public static ContainerSnapshot fromContainer(IContainer container) {
* parents.
*/

public static ContainerSnapshot fromVirtualFolderContainer(IContainer container) {
public static <R extends IContainer> ContainerSnapshot<? extends R> fromVirtualFolderContainer(R container) {
return fromContainer(container, true);
}

public static ContainerSnapshot fromContainer(IContainer container, boolean usingVirtualFolder) {
@SuppressWarnings("unchecked")
public static <R extends IContainer> ContainerSnapshot<? extends R> fromContainer(R container, boolean usingVirtualFolder) {
IPath fullPath = container.getFullPath();
ContainerSnapshot firstCreatedParent = null;
ContainerSnapshot currentContainerDescription = null;
ContainerSnapshot<? extends IContainer> firstCreatedParent = null;
ContainerSnapshot<? extends IContainer> currentContainerDescription = null;

// Does the container exist already? If so, then the parent exists and
// we use the normal creation constructor.
IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
IContainer currentContainer = (IContainer) root.findMember(fullPath);
if (currentContainer != null) {
return (ContainerSnapshot) ResourceSnapshotFactory
.fromResource(container);
return (ContainerSnapshot<R>) ResourceSnapshotFactory.fromResource(container);
}

// Create container descriptions for any uncreated parents in the given
Expand All @@ -105,12 +109,11 @@ public static ContainerSnapshot fromContainer(IContainer container, boolean usin
currentContainer = (IContainer) resource;
} else if (i == 0) {
// parent does not exist and it is a project
firstCreatedParent = new ProjectSnapshot(root
.getProject(currentSegment));
firstCreatedParent = new ProjectSnapshot(root.getProject(currentSegment));
currentContainerDescription = firstCreatedParent;
} else {
IFolder folderHandle = currentContainer.getFolder(IPath.fromOSString(currentSegment));
ContainerSnapshot currentFolder;
FolderSnapshot currentFolder;
currentFolder = new FolderSnapshot(folderHandle, usingVirtualFolder);
currentContainer = folderHandle;
if (currentContainerDescription != null) {
Expand All @@ -122,7 +125,7 @@ public static ContainerSnapshot fromContainer(IContainer container, boolean usin
}
}
}
return firstCreatedParent;
return (ContainerSnapshot<? extends R>)firstCreatedParent;
}

/**
Expand All @@ -139,7 +142,7 @@ public ContainerSnapshot() {
*
* @param container the container to be described
*/
public ContainerSnapshot(IContainer container) {
public ContainerSnapshot(T container) {
super(container);
this.name = container.getName();
if (container.isLinked()) {
Expand All @@ -149,10 +152,8 @@ public ContainerSnapshot(IContainer container) {
if (container.isAccessible()) {
defaultCharSet = container.getDefaultCharset(false);
IResource[] resourceMembers = container.members();
members = new AbstractResourceSnapshot[resourceMembers.length];
for (int i = 0; i < resourceMembers.length; i++) {
members[i] = (AbstractResourceSnapshot) ResourceSnapshotFactory
.fromResource(resourceMembers[i]);
for (IResource resourceMember : resourceMembers) {
members.add(ResourceSnapshotFactory.fromResource(resourceMember));
}
}
} catch (CoreException e) {
Expand All @@ -173,31 +174,29 @@ public ContainerSnapshot(IContainer container) {
protected final void createChildResources(IContainer parentHandle,
IProgressMonitor monitor) throws CoreException {
// restore any children
if (members != null && members.length > 0) {
SubMonitor subMonitor = SubMonitor.convert(monitor, members.length);
for (IResourceSnapshot member : members) {
if (member instanceof AbstractResourceSnapshot)
((AbstractResourceSnapshot) member).parent = parentHandle;
member.createResource(subMonitor.split(1));
}
SubMonitor subMonitor = SubMonitor.convert(monitor, members.size());
for (IResourceSnapshot<? extends IResource> member : members) {
if (member instanceof AbstractResourceSnapshot)
((AbstractResourceSnapshot<? extends IResource>) member).parent = parentHandle;
member.createResource(subMonitor.split(1));
}
}

@Override
public void recordStateFromHistory(IResource resource, IProgressMonitor mon) throws CoreException {
public void recordStateFromHistory(T resource, IProgressMonitor mon) throws CoreException {
if (members != null) {
SubMonitor subMonitor = SubMonitor.convert(mon, ResourceSnapshotMessages.FolderDescription_SavingUndoInfoProgress,
members.length);
for (IResourceSnapshot member : members) {
members.size());
for (IResourceSnapshot<? extends IResource> member : members) {
SubMonitor iterationMonitor = subMonitor.split(1);
if (member instanceof FileSnapshot) {
IPath path = resource.getFullPath().append(((FileSnapshot) member).name);
if (member instanceof FileSnapshot fileSnapshot) {
IPath path = resource.getFullPath().append(fileSnapshot.name);
IFile fileHandle = resource.getWorkspace().getRoot().getFile(path);
member.recordStateFromHistory(fileHandle, iterationMonitor);
} else if (member instanceof FolderSnapshot) {
IPath path = resource.getFullPath().append(((FolderSnapshot) member).name);
fileSnapshot.recordStateFromHistory(fileHandle, iterationMonitor);
} else if (member instanceof FolderSnapshot folderSnapshot) {
IPath path = resource.getFullPath().append(folderSnapshot.name);
IFolder folderHandle = resource.getWorkspace().getRoot().getFolder(path);
member.recordStateFromHistory(folderHandle, iterationMonitor);
folderSnapshot.recordStateFromHistory(folderHandle, iterationMonitor);
}
}
}
Expand All @@ -213,11 +212,10 @@ public String getName() {
return name;
}

@SuppressWarnings("unchecked")
@Override
public IResourceSnapshot[] getMembers() {
AbstractResourceSnapshot[] clone = new AbstractResourceSnapshot[members.length];
System.arraycopy(members, 0, clone, 0, members.length);
return clone;
public IResourceSnapshot<? extends IResource>[] getMembers() {
return members.toArray((IResourceSnapshot<? extends IResource>[]) Array.newInstance(IResourceSnapshot.class, members.size()));
}

/**
Expand All @@ -226,15 +224,8 @@ public IResourceSnapshot[] getMembers() {
* @param member the resource snapshot considered a member of this container.
*/
@Override
public void addMember(IResourceSnapshot member) {
if (members == null) {
members = new IResourceSnapshot[] { member };
} else {
IResourceSnapshot[] expandedMembers = new AbstractResourceSnapshot[members.length + 1];
System.arraycopy(members, 0, expandedMembers, 0, members.length);
expandedMembers[members.length] = member;
members = expandedMembers;
}
public void addMember(IResourceSnapshot<? extends IResource> member) {
members.add(member);
}

@Override
Expand Down Expand Up @@ -274,11 +265,9 @@ public boolean verifyExistence(boolean checkMembers) {
if (existence) {
if (checkMembers) {
// restore any children
if (members != null && members.length > 0) {
for (IResourceSnapshot member : members) {
if (!member.verifyExistence(checkMembers)) {
return false;
}
for (IResourceSnapshot<? extends IResource> member : members) {
if (!member.verifyExistence(checkMembers)) {
return false;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
* @since 3.20
*
*/
public class FileSnapshot extends AbstractResourceSnapshot {
public class FileSnapshot extends AbstractResourceSnapshot<IFile> {

String name;

Expand Down Expand Up @@ -94,15 +94,14 @@ public FileSnapshot(IFile file, URI linkLocation,
}

@Override
public void recordStateFromHistory(IResource resource,
public void recordStateFromHistory(IFile resource,
IProgressMonitor monitor) throws CoreException {
Assert.isLegal(resource.getType() == IResource.FILE);

if (location != null) {
// file is linked, no need to record any history
return;
}
IFileState[] states = ((IFile) resource).getHistory(monitor);
IFileState[] states = resource.getHistory(monitor);
if (states.length > 0) {
final IFileState state = getMatchingFileState(states);
this.fileContentDescription = new IFileContentSnapshot() {
Expand All @@ -125,20 +124,17 @@ public String getCharset() throws CoreException {
}

@Override
public IResource createResourceHandle() {
public IFile createResourceHandle() {
IWorkspaceRoot workspaceRoot = parent.getWorkspace().getRoot();
IPath fullPath = parent.getFullPath().append(name);
return workspaceRoot.getFile(fullPath);
}

@Override
public void createExistentResourceFromHandle(IResource resource, IProgressMonitor mon) throws CoreException {

Assert.isLegal(resource instanceof IFile);
if (resource.exists()) {
public void createExistentResourceFromHandle(IFile fileHandle, IProgressMonitor mon) throws CoreException {
if (fileHandle.exists()) {
return;
}
IFile fileHandle = (IFile) resource;
SubMonitor subMonitor = SubMonitor.convert(mon, 200);
subMonitor.setTaskName(ResourceSnapshotMessages.FileDescription_NewFileProgress);
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
package org.eclipse.core.internal.resources.undo.snapshot;

import java.net.URI;

import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IResourceFilterDescription;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
Expand All @@ -35,7 +35,7 @@
* @since 3.20
*
*/
public class FolderSnapshot extends ContainerSnapshot {
public class FolderSnapshot extends ContainerSnapshot<IFolder> {

private boolean virtual = false;

Expand Down Expand Up @@ -67,19 +67,17 @@ public FolderSnapshot(IFolder folder, URI linkLocation) {
}

@Override
public IResource createResourceHandle() {
public IFolder createResourceHandle() {
IWorkspaceRoot workspaceRoot = getWorkspace().getRoot();
IPath folderPath = parent.getFullPath().append(name);
return workspaceRoot.getFolder(folderPath);
}

@Override
public void createExistentResourceFromHandle(IResource resource, IProgressMonitor mon) throws CoreException {
Assert.isLegal(resource instanceof IFolder);
if (resource.exists()) {
public void createExistentResourceFromHandle(final IFolder folderHandle, IProgressMonitor mon) throws CoreException {
if (folderHandle.exists()) {
return;
}
IFolder folderHandle = (IFolder) resource;
SubMonitor subMonitor = SubMonitor.convert(mon, 300);
subMonitor.setTaskName(ResourceSnapshotMessages.FolderDescription_NewFolderProgress);
if (filters != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
* @since 3.20
*
*/
public class ProjectSnapshot extends ContainerSnapshot {
public class ProjectSnapshot extends ContainerSnapshot<IProject> {

private IProjectDescription projectDescription;
private boolean openOnCreate = true;
Expand Down Expand Up @@ -73,19 +73,16 @@ public ProjectSnapshot(IProjectDescription projectDescription) {
}

@Override
public IResource createResourceHandle() {
public IProject createResourceHandle() {
return ResourcesPlugin.getWorkspace().getRoot().getProject(getName());
}

@Override
public void createExistentResourceFromHandle(IResource resource,
IProgressMonitor monitor) throws CoreException {
public void createExistentResourceFromHandle(IProject projectHandle, IProgressMonitor monitor) throws CoreException {
SubMonitor subMonitor = SubMonitor.convert(monitor, 200);
Assert.isLegal(resource instanceof IProject);
if (resource.exists()) {
if (projectHandle.exists()) {
return;
}
IProject projectHandle = (IProject) resource;
subMonitor.setTaskName(ResourceSnapshotMessages.FolderDescription_NewFolderProgress);
if (projectDescription == null) {
projectHandle.create(subMonitor.split(100));
Expand All @@ -109,7 +106,7 @@ public String getName() {
@Override
public boolean verifyExistence(boolean checkMembers) {
// We can only check members if the project is open.
IProject projectHandle = (IProject) createResourceHandle();
IProject projectHandle = createResourceHandle();
if (projectHandle.isAccessible()) {
return super.verifyExistence(checkMembers);
}
Expand Down
Loading

0 comments on commit 26702b2

Please sign in to comment.