Skip to content

Commit

Permalink
Minor code simplifications in LaunchConfiguration
Browse files Browse the repository at this point in the history
  • Loading branch information
HannesWell committed Sep 30, 2024
1 parent 2f90d3c commit 007ee10
Showing 1 changed file with 12 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

import javax.xml.parsers.DocumentBuilder;
Expand Down Expand Up @@ -239,13 +240,12 @@ protected static String getSimpleName(String fileName) {
protected LaunchConfiguration(String memento) throws CoreException {
Exception ex = null;
try {
Element root = null;
@SuppressWarnings("restriction")
DocumentBuilder parser = org.eclipse.core.internal.runtime.XmlProcessorFactory.createDocumentBuilderWithErrorOnDOCTYPE();
parser.setErrorHandler(new DefaultHandler());
StringReader reader = new StringReader(memento);
InputSource source = new InputSource(reader);
root = parser.parse(source).getDocumentElement();
Element root = parser.parse(source).getDocumentElement();

String localString = root.getAttribute(IConfigurationElementConstants.LOCAL);
String path = root.getAttribute(IConfigurationElementConstants.PATH);
Expand Down Expand Up @@ -285,23 +285,19 @@ protected LaunchConfiguration(String memento) throws CoreException {
@Override
public boolean contentsEqual(ILaunchConfiguration object) {
try {
if (object instanceof LaunchConfiguration) {
LaunchConfiguration otherConfig = (LaunchConfiguration) object;
return getName().equals(otherConfig.getName())
&& getType().equals(otherConfig.getType())
&& equalOrNull(getContainer(), otherConfig.getContainer())
&& getInfo().equals(otherConfig.getInfo());
}
return false;
return object instanceof LaunchConfiguration otherConfig //
&& getName().equals(otherConfig.getName()) //
&& getType().equals(otherConfig.getType()) //
&& Objects.equals(getContainer(), otherConfig.getContainer()) //
&& getInfo().equals(otherConfig.getInfo());
} catch (CoreException ce) {
return false;
}
}

@Override
public ILaunchConfigurationWorkingCopy copy(String name) throws CoreException {
ILaunchConfigurationWorkingCopy copy = new LaunchConfigurationWorkingCopy(this, name);
return copy;
return new LaunchConfigurationWorkingCopy(this, name);
}

@Override
Expand Down Expand Up @@ -362,36 +358,17 @@ public void delete(int flag) throws CoreException {
*/
@Override
public boolean equals(Object object) {
if (object instanceof ILaunchConfiguration) {
if (object instanceof LaunchConfiguration config) {
if (isWorkingCopy()) {
return this == object;
}
LaunchConfiguration config = (LaunchConfiguration) object;
if (!config.isWorkingCopy()) {
return getName().equals(config.getName()) &&
equalOrNull(getContainer(), config.getContainer());
return getName().equals(config.getName()) && Objects.equals(getContainer(), config.getContainer());
}
}
return false;
}

/**
* Returns whether the given objects are equal or both <code>null</code>.
*
* @param o1 the object
* @param o2 the object to be compared to o1
* @return whether the given objects are equal or both <code>null</code>
* @since 3.5
*/
protected boolean equalOrNull(Object o1, Object o2) {
if (o1 == null) {
return o2 == null;
} else if (o2 != null) {
return o1.equals(o2);
}
return false;
}

@Override
public boolean exists() {
IFile file = getFile();
Expand Down Expand Up @@ -542,7 +519,7 @@ public IResource[] getMappedResources() throws CoreException {
if (types == null || types.size() != paths.size()) {
throw new CoreException(newStatus(DebugCoreMessages.LaunchConfiguration_0, DebugPlugin.ERROR, null));
}
ArrayList<IResource> list = new ArrayList<>();
List<IResource> list = new ArrayList<>();
IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
for(int i = 0; i < paths.size(); i++) {
String pathStr = paths.get(i);
Expand Down Expand Up @@ -650,12 +627,7 @@ public ILaunchConfigurationWorkingCopy getWorkingCopy() throws CoreException {

@Override
public int hashCode() {
IContainer container = getContainer();
if (container == null) {
return getName().hashCode();
} else {
return getName().hashCode() + container.hashCode();
}
return Objects.hash(getName(), getContainer());
}

@Override
Expand Down

0 comments on commit 007ee10

Please sign in to comment.