Skip to content

Commit

Permalink
Code improvements, clean-ups and replacement of deprecated methods
Browse files Browse the repository at this point in the history
  • Loading branch information
HannesWell committed Jul 2, 2023
1 parent 57b1d85 commit ee347dc
Show file tree
Hide file tree
Showing 20 changed files with 152 additions and 329 deletions.
33 changes: 5 additions & 28 deletions org.eclipse.tips.core/src/org/eclipse/tips/core/Tip.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
* This is the base Tip class of the UI agnostic Tip framework. You might want
Expand Down Expand Up @@ -69,12 +70,7 @@ public List<TipAction> getActions() {

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((getCreationDate() == null) ? 0 : getCreationDate().hashCode());
result = prime * result + ((providerId == null) ? 0 : providerId.hashCode());
result = prime * result + ((getSubject() == null) ? 0 : getSubject().hashCode());
return result;
return Objects.hash(getCreationDate(), providerId, getSubject());
}

@Override
Expand All @@ -89,27 +85,8 @@ public boolean equals(Object obj) {
return false;
}
Tip other = (Tip) obj;
if (getCreationDate() == null) {
if (other.getCreationDate() != null) {
return false;
}
} else if (!getCreationDate().equals(other.getCreationDate())) {
return false;
}
if (providerId == null) {
if (other.providerId != null) {
return false;
}
} else if (!providerId.equals(other.providerId)) {
return false;
}
if (getSubject() == null) {
if (other.getSubject() != null) {
return false;
}
} else if (!getSubject().equals(other.getSubject())) {
return false;
}
return true;
return Objects.equals(getCreationDate(), other.getCreationDate()) //
&& Objects.equals(providerId, other.providerId) //
&& Objects.equals(getSubject(), other.getSubject());
}
}
18 changes: 6 additions & 12 deletions org.eclipse.tips.core/src/org/eclipse/tips/core/TipImage.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
*****************************************************************************/
package org.eclipse.tips.core;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
Expand All @@ -40,12 +39,10 @@ public class TipImage {
private String fExtension = null;
private int fMaxWidth = UNDEFINED;
private int fMaxHeight = UNDEFINED;
final private URL fURL;
private final URL fURL;
private double fAspectRatio = THREE_TO_TWO;

final private String fBase64Image;

private static final int _4KB = 4096;
private final String fBase64Image;

/**
* Creates a new TipImage with the specified URL which gets read into a base 64
Expand All @@ -60,17 +57,14 @@ public class TipImage {
public TipImage(URL url) throws IOException {
Assert.isNotNull(url);
fURL = url;
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
byte[] chunk = new byte[_4KB];
int bytesRead;
InputStream stream = url.openStream();
while ((bytesRead = stream.read(chunk)) > 0) {
outputStream.write(chunk, 0, bytesRead);
byte[] bytes;
try (InputStream in = url.openStream()) {
bytes = in.readAllBytes();
}
fBase64Image = "data:image/" // //$NON-NLS-1$
+ getExtension() //
+ ";base64," // //$NON-NLS-1$
+ Base64.getEncoder().encodeToString(outputStream.toByteArray());
+ Base64.getEncoder().encodeToString(bytes);
}

/**
Expand Down
11 changes: 3 additions & 8 deletions org.eclipse.tips.core/src/org/eclipse/tips/core/TipProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import java.util.Comparator;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
Expand Down Expand Up @@ -139,9 +138,7 @@ public synchronized Tip getNextTip() {
if (list.isEmpty()) {
return setCurrentTip(fFinalTip);
}
if (getManager().mustServeReadTips() && fCurrentTip != null) {
fTipIndex++;
} else if (fCurrentTip != null && getManager().isRead(fCurrentTip)) {
if (fCurrentTip != null && (getManager().mustServeReadTips() || getManager().isRead(fCurrentTip))) {
fTipIndex++;
}
if (fTipIndex >= list.size()) {
Expand Down Expand Up @@ -246,11 +243,9 @@ public List<Tip> getTips() {
*/
public synchronized List<Tip> getTips(Predicate<Tip> predicate) {
if (predicate != null) {
return Collections.unmodifiableList(fTips //
.stream() //
.filter(tip -> predicate.test(tip)) //
return fTips.stream().filter(predicate) //
.sorted(Comparator.comparing(Tip::getCreationDate).reversed()) //
.collect(Collectors.toList()));
.toList();
}
return Collections.unmodifiableList(fTips);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,24 +134,21 @@ private synchronized void addToMaps(TipProvider pProvider, Integer pPriorityHint
}

private void addToPriorityMap(TipProvider provider, Integer priorityHint) {
if (!fProviderPrio.get(priorityHint).contains(provider.getID())) {
if (!fProviderPrio.get(priorityHint).contains(provider.getID())) {
fProviderPrio.get(priorityHint).add(provider.getID());
}
List<String> providers = fProviderPrio.get(priorityHint);
if (!providers.contains(provider.getID())) {
providers.add(provider.getID());
}
}

private void addToProviderMaps(TipProvider provider, Integer priorityHint) {
fProviders.put(provider.getID(), provider);
if (fProviderPrio.get(priorityHint) == null) {
fProviderPrio.put(priorityHint, new ArrayList<>());
}
fProviderPrio.computeIfAbsent(priorityHint, p -> new ArrayList<>());
}

private void removeFromMaps(TipProvider provider) {
if (fProviders.containsKey(provider.getID())) {
for (Map.Entry<Integer, List<String>> entry : fProviderPrio.entrySet()) {
entry.getValue().remove(provider.getID());
for (List<String> providers : fProviderPrio.values()) {
providers.remove(provider.getID());
}
fProviders.remove(provider.getID());
}
Expand All @@ -169,15 +166,9 @@ public List<TipProvider> getProviders() {
if (fProviders == null) {
return Collections.emptyList();
}
ArrayList<TipProvider> result = new ArrayList<>();
for (Map.Entry<Integer, List<String>> entry : fProviderPrio.entrySet()) {
for (String id : entry.getValue()) {
if (fProviders.get(id).isReady()) {
result.add(fProviders.get(id));
}
}
}
return Collections.unmodifiableList(result);
return fProviderPrio.values().stream().flatMap(List::stream) //
.map(fProviders::get).filter(TipProvider::isReady) //
.toList();
}

/**
Expand Down Expand Up @@ -278,11 +269,6 @@ public boolean isDisposed() {

@Override
public boolean hasContent() {
for (TipProvider provider : getProviders()) {
if (provider.isReady() && !provider.getTips(tip -> !isRead(tip)).isEmpty()) {
return true;
}
}
return false;
return getProviders().stream().anyMatch(p -> p.isReady() && !p.getTips(tip -> !isRead(tip)).isEmpty());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -121,16 +121,10 @@ public ITipManager open(boolean startUp) {
*
*/
private void saveReadState(Map<String, List<Integer>> pReadTips) {
Job job = new Job(Messages.IDETipManager_3) {
@Override
protected IStatus run(IProgressMonitor monitor) {
SubMonitor subMonitor = SubMonitor.convert(monitor, IProgressMonitor.UNKNOWN);
subMonitor.setTaskName(Messages.IDETipManager_4);
IStatus status = TipsPreferences.saveReadState(pReadTips);
subMonitor.done();
return status;
}
};
Job job = Job.create(Messages.IDETipManager_3, monitor -> {
SubMonitor.convert(monitor, Messages.IDETipManager_4, IProgressMonitor.UNKNOWN);
return TipsPreferences.saveReadState(pReadTips);
});
job.schedule();
}

Expand Down Expand Up @@ -193,22 +187,14 @@ public ITipManager log(IStatus status) {

@Override
public boolean isRead(Tip tip) {
if (fReadTips.containsKey(tip.getProviderId())
&& fReadTips.get(tip.getProviderId()).contains(Integer.valueOf(tip.hashCode()))) {
return true;
}
return false;
return fReadTips.getOrDefault(tip.getProviderId(), List.of()).contains(tip.hashCode());
}

@Override
public synchronized TipManager setAsRead(Tip tip) {
if (!isRead(tip)) {
List<Integer> readTips = fReadTips.get(tip.getProviderId());
if (readTips == null) {
readTips = new ArrayList<>();
fReadTips.put(tip.getProviderId(), readTips);
}
readTips.add(Integer.valueOf(tip.hashCode()));
List<Integer> readTips = fReadTips.computeIfAbsent(tip.getProviderId(), i -> new ArrayList<>());
readTips.add(tip.hashCode());
}
return this;
}
Expand Down Expand Up @@ -265,11 +251,7 @@ private int doGetPriority(String expression) {
Element element = (Element) doc.getElementsByTagName("enablement").item(0); //$NON-NLS-1$
Expression expressionObj = ExpressionConverter.getDefault().perform(element);
final EvaluationResult result = expressionObj.evaluate(getEvaluationContext());
if (result == EvaluationResult.TRUE) {
return 10;
} else {
return 30;
}
return EvaluationResult.TRUE.equals(result) ? 10 : 30;
} catch (Exception e) {
log(LogUtil.error(e));
return 20;
Expand All @@ -282,8 +264,7 @@ private int doGetPriority(String expression) {
*/
private static IEvaluationContext getEvaluationContext() {
IEvaluationService evalService = PlatformUI.getWorkbench().getService(IEvaluationService.class);
IEvaluationContext currentState = evalService.getCurrentState();
return currentState;
return evalService.getCurrentState();
}

/**
Expand All @@ -294,7 +275,7 @@ private static IEvaluationContext getEvaluationContext() {
* @return the state location file
* @throws Exception if something went wrong
*/
public static File getStateLocation() throws Exception {
public static File getStateLocation() throws IOException {
String stateLocation = System.getProperty("org.eclipse.tips.statelocation"); //$NON-NLS-1$
if (stateLocation == null) {
stateLocation = System.getProperty("user.home") + File.separator + ".eclipse" + File.separator //$NON-NLS-1$ //$NON-NLS-2$
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,6 @@ public class TipsPreferences extends AbstractPreferenceInitializer {
*/
public static final String PREF_DISABLED_PROVIDERS = "disabled_providers"; //$NON-NLS-1$

public TipsPreferences() {
}

@Override
public void initializeDefaultPreferences() {
// nothing needs to be done right now
Expand All @@ -123,14 +120,16 @@ public void initializeDefaultPreferences() {
* @return a map that stores the read tip hashes per provider.
*/
public static Map<String, List<Integer>> getReadState() {
HashMap<String, List<Integer>> result = new HashMap<>();
Map<String, List<Integer>> result = new HashMap<>();

try {
File stateLocation = getStateLocation();
for (String key : stateLocation.list(getStateFileNameFilter(stateLocation))) {
FilenameFilter stateFileNameFilter = (pDir, pName) -> pDir.equals(stateLocation)
&& pName.endsWith(".state"); //$NON-NLS-1$
for (String key : stateLocation.list(stateFileNameFilter)) {
PreferenceStore store = new PreferenceStore(new File(stateLocation, key).getAbsolutePath());
store.load();
ArrayList<Integer> tips = new ArrayList<>();
List<Integer> tips = new ArrayList<>();
for (String tipKey : store.preferenceNames()) {
if (!"provider".equals(tipKey)) { //$NON-NLS-1$
tips.add(Integer.valueOf(store.getInt(tipKey)));
Expand All @@ -139,21 +138,11 @@ public static Map<String, List<Integer>> getReadState() {
result.put(store.getString("provider"), tips); //$NON-NLS-1$
}
} catch (Exception e) {
Status status = new Status(IStatus.ERROR, "org.eclipse.tips.ide", e.getMessage(), e); //$NON-NLS-1$
log(status);
log(Status.error(e.getMessage(), e));
}
return result;
}

private static FilenameFilter getStateFileNameFilter(File stateLocation) {
return (pDir, pName) -> {
if (pDir.equals(stateLocation) && pName.endsWith(".state")) { //$NON-NLS-1$
return true;
}
return false;
};
}

private static File getStateLocation() throws Exception {
File file = new File(IDETipManager.getStateLocation(), "org.eclipse.tips.ide.state"); //$NON-NLS-1$
if (!file.exists()) {
Expand Down Expand Up @@ -243,22 +232,18 @@ public static int getStartupBehavior() {
if (behavior == null) {
behavior = getDefaultStartupBehavior();
}
switch (behavior) {
case PREF_STARTUP_BEHAVIOR_DIALOG:
return TipManager.START_DIALOG;
case PREF_STARTUP_BEHAVIOR_BACKGROUND:
return TipManager.START_BACKGROUND;
case PREF_STARTUP_BEHAVIOR_DISABLE:
return TipManager.START_DISABLE;
default:
return TipManager.START_BACKGROUND;
}
return switch (behavior) {
case PREF_STARTUP_BEHAVIOR_DIALOG -> TipManager.START_DIALOG;
case PREF_STARTUP_BEHAVIOR_BACKGROUND -> TipManager.START_BACKGROUND;
case PREF_STARTUP_BEHAVIOR_DISABLE -> TipManager.START_DISABLE;
default -> TipManager.START_BACKGROUND;
};
}

private static String getDefaultStartupBehavior() {
String startupBehavior = System.getProperty(PREF_STARTUP_BEHAVIOR_PROPERTY);
if (startupBehavior == null) {
return startupBehavior = DefaultScope.INSTANCE.getNode(Constants.BUNDLE_ID).get(PREF_STARTUP_BEHAVIOR,
return DefaultScope.INSTANCE.getNode(Constants.BUNDLE_ID).get(PREF_STARTUP_BEHAVIOR,
PREF_STARTUP_BEHAVIOR_BACKGROUND);
}
return startupBehavior;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,11 @@
package org.eclipse.tips.ide.internal.provider;

import java.util.Date;
import java.util.Optional;

import org.eclipse.tips.core.IHtmlTip;
import org.eclipse.tips.core.Tip;
import org.eclipse.tips.core.TipImage;
import org.osgi.framework.Bundle;
import org.osgi.framework.FrameworkUtil;

public class Tip1_Welcome extends Tip implements IHtmlTip {

Expand All @@ -31,7 +30,7 @@ public Tip1_Welcome(String providerId) {

@Override
public Date getCreationDate() {
return TipsTipProvider.getDateFromYYMMDD("09/01/2019");
return TipsTipProvider.getDateFromYYMMDD(9, 1, 2019);
}

@Override
Expand All @@ -49,11 +48,8 @@ public String getHTML() {
@Override
public TipImage getImage() {
if (fImage == null) {
try {
Bundle bundle = FrameworkUtil.getBundle(getClass());
fImage = new TipImage(bundle.getEntry("images/tips/welcome.png")).setAspectRatio(560, 480, true);
} catch (Exception e) {
}
Optional<TipImage> tipImage = TipsTipProvider.getTipImage("images/tips/welcome.png"); //$NON-NLS-1$
fImage = tipImage.map(i -> i.setAspectRatio(560, 480, true)).orElse(null);
}
return fImage;
}
Expand Down
Loading

0 comments on commit ee347dc

Please sign in to comment.