Skip to content

Commit

Permalink
IMessageCheck now returns an IMessageCheckResult instead of a boolean
Browse files Browse the repository at this point in the history
  • Loading branch information
claesrosell committed Oct 23, 2023
1 parent 049775e commit 86b5280
Show file tree
Hide file tree
Showing 15 changed files with 165 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ public interface IMessageCheck {
* messages bundle group
* @param message
* the message being tested
* @return <code>true</code> if condition is successfully tested
* @return <code>MessageCheckResult.OK</code> if condition is successfully tested
*/
boolean checkKey(IMessagesBundleGroup messagesBundleGroup, IMessage message);
IMessageCheckResult checkKey(IMessagesBundleGroup messagesBundleGroup, IMessage message);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package org.eclipse.babel.core.message.checks;

public interface IMessageCheckResult {
String getText();
IMessageCheck getMessageCheck();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.eclipse.babel.core.message.checks;

public class MessageCheckResult implements IMessageCheckResult {

public static final IMessageCheckResult OK = new MessageCheckResult("", null);//$NON-NLS-1$

private String text;
private IMessageCheck messageCheck;

public MessageCheckResult(String text, IMessageCheck messageCheck) {
this.text = text;
this.messageCheck = messageCheck;
}

@Override
public String getText() {
return this.text;
}

@Override
public IMessageCheck getMessageCheck() {
return this.messageCheck;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import org.eclipse.babel.core.message.IMessagesBundle;
import org.eclipse.babel.core.message.IMessagesBundleGroup;
import org.eclipse.babel.core.message.checks.IMessageCheck;
import org.eclipse.babel.core.message.checks.IMessageCheckResult;
import org.eclipse.babel.core.message.checks.MessageCheckResult;
import org.eclipse.babel.core.util.BabelUtils;

/**
Expand All @@ -26,25 +28,18 @@
*/
public class DuplicateValueCheck implements IMessageCheck {

private String[] duplicateKeys;
public static final DuplicateValueCheck INSTANCE = new DuplicateValueCheck();

/**
* Constructor.
*/
public DuplicateValueCheck() {
private DuplicateValueCheck() {
super();
}

/**
* Resets the collected keys to null.
*/
public void reset() {
duplicateKeys = null;
}

public boolean checkKey(IMessagesBundleGroup messagesBundleGroup,
public IMessageCheckResult checkKey(IMessagesBundleGroup messagesBundleGroup,
IMessage message) {
Collection<String> keys = new ArrayList<String>();
Collection<String> keys = new ArrayList<>();
if (message != null) {
IMessagesBundle messagesBundle = messagesBundleGroup
.getMessagesBundle(message.getLocale());
Expand All @@ -60,12 +55,10 @@ public boolean checkKey(IMessagesBundleGroup messagesBundleGroup,
}
}

duplicateKeys = keys.toArray(new String[] {});
return !keys.isEmpty();
}

public String[] getDuplicateKeys() {
return duplicateKeys;
if ( keys.isEmpty() ) {
return MessageCheckResult.OK;
} else {
return new DuplicateValueMessageCheckResult(keys.toArray(String[]::new), this);
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.eclipse.babel.core.message.checks.internal;

import org.eclipse.babel.core.message.checks.IMessageCheck;
import org.eclipse.babel.core.message.checks.MessageCheckResult;

public class DuplicateValueMessageCheckResult extends MessageCheckResult {

private String[] duplicateKeys;

public DuplicateValueMessageCheckResult(String[] duplicateKeys, IMessageCheck messageCheck) {
super("", messageCheck);
this.duplicateKeys = duplicateKeys;
}

public String[] getDuplicateKeys() {
return this.duplicateKeys;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import org.eclipse.babel.core.message.IMessage;
import org.eclipse.babel.core.message.IMessagesBundleGroup;
import org.eclipse.babel.core.message.checks.IMessageCheck;
import org.eclipse.babel.core.message.checks.IMessageCheckResult;
import org.eclipse.babel.core.message.checks.MessageCheckResult;

/**
* Visitor for finding if a key has at least one corresponding bundle entry with
Expand All @@ -23,7 +25,7 @@
public class MissingValueCheck implements IMessageCheck {

/** The singleton */
public static MissingValueCheck MISSING_KEY = new MissingValueCheck();
public static final MissingValueCheck INSTANCE = new MissingValueCheck();

/**
* Constructor.
Expand All @@ -36,12 +38,13 @@ private MissingValueCheck() {
* @see org.eclipse.babel.core.message.internal.checks.IMessageCheck#checkKey(org.eclipse.babel.core.message.internal.MessagesBundleGroup,
* org.eclipse.babel.core.message.internal.Message)
*/
public boolean checkKey(IMessagesBundleGroup messagesBundleGroup,
public IMessageCheckResult checkKey(IMessagesBundleGroup messagesBundleGroup,
IMessage message) {
if (message == null || message.getValue() == null
|| message.getValue().length() == 0) {
return true;
return MessageCheckResult.OK;
}
return false;
return new MissingValueCheckResult(this);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.eclipse.babel.core.message.checks.internal;

import org.eclipse.babel.core.message.checks.IMessageCheck;
import org.eclipse.babel.core.message.checks.MessageCheckResult;

public class MissingValueCheckResult extends MessageCheckResult {

public MissingValueCheckResult(IMessageCheck messageCheck) {
super("", messageCheck);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@
import org.eclipse.babel.core.message.IMessagesBundle;
import org.eclipse.babel.core.message.IMessagesBundleGroup;
import org.eclipse.babel.core.message.checks.IMessageCheck;
import org.eclipse.babel.core.message.checks.IMessageCheckResult;
import org.eclipse.babel.core.message.checks.MessageCheckResult;
import org.eclipse.babel.core.message.checks.proximity.IProximityAnalyzer;
import org.eclipse.babel.core.message.checks.proximity.LevenshteinDistanceAnalyzer;
import org.eclipse.babel.core.util.BabelUtils;

/**
Expand All @@ -30,10 +33,13 @@ public class SimilarValueCheck implements IMessageCheck {
private String[] similarKeys;
private IProximityAnalyzer analyzer;

/** The singleton */
public static final SimilarValueCheck INSTANCE = new SimilarValueCheck(LevenshteinDistanceAnalyzer.getInstance());

/**
* Constructor.
*/
public SimilarValueCheck(IProximityAnalyzer analyzer) {
private SimilarValueCheck(IProximityAnalyzer analyzer) {
super();
this.analyzer = analyzer;
}
Expand All @@ -42,7 +48,7 @@ public SimilarValueCheck(IProximityAnalyzer analyzer) {
* @see org.eclipse.babel.core.message.internal.checks.IMessageCheck#checkKey(org.eclipse.babel.core.message.internal.MessagesBundleGroup,
* org.eclipse.babel.core.message.internal.Message)
*/
public boolean checkKey(IMessagesBundleGroup messagesBundleGroup,
public IMessageCheckResult checkKey(IMessagesBundleGroup messagesBundleGroup,
IMessage message) {
Collection<String> keys = new ArrayList<String>();
if (message != null) {
Expand All @@ -67,16 +73,11 @@ public boolean checkKey(IMessagesBundleGroup messagesBundleGroup,
keys.add(message.getKey());
}
}
similarKeys = keys.toArray(new String[] {});
return !keys.isEmpty();
}

/**
* Gets similar keys.
*
* @return similar keys
*/
public String[] getSimilarMessageKeys() {
return similarKeys;
if ( !keys.isEmpty() ) {
return MessageCheckResult.OK;
} else {
return new SimilarValueMessageCheckResult(keys.toArray(String[]::new), this);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.eclipse.babel.core.message.checks.internal;

import org.eclipse.babel.core.message.checks.IMessageCheck;
import org.eclipse.babel.core.message.checks.MessageCheckResult;

public class SimilarValueMessageCheckResult extends MessageCheckResult {

private String[] similarValueKeys;

public SimilarValueMessageCheckResult(String[] similarValueKeys, IMessageCheck messageCheck) {
super("", messageCheck);
this.similarValueKeys = similarValueKeys;
}

public String[] getSimilarMessageKeys() {
return similarValueKeys;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@
import java.util.Observer;

import org.eclipse.babel.core.message.checks.IMessageCheck;
import org.eclipse.babel.core.message.checks.IMessageCheckResult;
import org.eclipse.babel.core.message.checks.internal.DuplicateValueCheck;
import org.eclipse.babel.core.message.checks.internal.DuplicateValueMessageCheckResult;
import org.eclipse.babel.core.message.checks.internal.MissingValueCheck;
import org.eclipse.babel.core.message.checks.internal.MissingValueCheckResult;
import org.eclipse.babel.editor.IMessagesEditorChangeListener;
import org.eclipse.babel.editor.i18n.actions.ShowDuplicateAction;
import org.eclipse.babel.editor.i18n.actions.ShowMissingAction;
Expand Down Expand Up @@ -116,10 +119,10 @@ public void run() {
toolBarMgr.removeAll();
actionByMarkerIds.clear();
String key = editor.getSelectedKey();
Collection<IMessageCheck> checks = editor.getMarkers()
Collection<IMessageCheckResult> checks = editor.getMarkers()
.getFailedChecks(key, locale);
if (checks != null) {
for (IMessageCheck check : checks) {
for (IMessageCheckResult check : checks) {
Action action = getCheckAction(key, check);
if (action != null) {
toolBarMgr.add(action);
Expand All @@ -141,12 +144,12 @@ public void run() {

}

private Action getCheckAction(String key, IMessageCheck check) {
if (check instanceof MissingValueCheck) {
private Action getCheckAction(String key, IMessageCheckResult check) {
if (check instanceof MissingValueCheckResult checkResult) {
return new ShowMissingAction(key, locale);
} else if (check instanceof DuplicateValueCheck) {
} else if (check instanceof DuplicateValueMessageCheckResult checkResult) {
return new ShowDuplicateAction(
((DuplicateValueCheck) check).getDuplicateKeys(), key,
checkResult.getDuplicateKeys(), key,
locale);
}
return null;
Expand Down
Loading

0 comments on commit 86b5280

Please sign in to comment.