Skip to content

Commit

Permalink
ZNTA-2057 refactor async task key
Browse files Browse the repository at this point in the history
  • Loading branch information
Patrick Huang committed Jun 15, 2017
1 parent b39160f commit 978639f
Show file tree
Hide file tree
Showing 9 changed files with 210 additions and 219 deletions.
Expand Up @@ -21,11 +21,12 @@
package org.zanata.action;

import java.io.Serializable;
import java.util.List;

import javax.annotation.Nonnull;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import javax.enterprise.context.Dependent;
import javax.inject.Inject;
import javax.inject.Named;

import org.zanata.async.AsyncTaskHandle;
import org.zanata.async.AsyncTaskHandleManager;
import org.zanata.async.handle.CopyTransTaskHandle;
Expand All @@ -34,6 +35,10 @@
import org.zanata.model.HProjectIteration;
import org.zanata.security.ZanataIdentity;
import org.zanata.service.CopyTransService;

import com.google.common.base.Preconditions;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
// TODO This class should be merged with the copy trans service (?)

/**
Expand Down Expand Up @@ -145,8 +150,10 @@ public void cancelCopyTrans(@Nonnull HProjectIteration iteration) {
/**
* Internal class to index Copy Trans processes.
*/
private static final class CopyTransProcessKey implements Serializable {
private static final class CopyTransProcessKey implements
AsyncTaskHandleManager.AsyncTaskKey<CopyTransProcessKey> {
private static final long serialVersionUID = -2054359069473618887L;
private static final String KEY_NAME = "copyTransKey";
private String projectSlug;
private String iterationSlug;
private String docId;
Expand All @@ -167,46 +174,6 @@ public static CopyTransProcessKey getKey(HDocument document) {
return newKey;
}

@Override
public boolean equals(final Object o) {
if (o == this)
return true;
if (!(o instanceof CopyTransManager.CopyTransProcessKey))
return false;
final CopyTransProcessKey other = (CopyTransProcessKey) o;
final Object this$projectSlug = this.getProjectSlug();
final Object other$projectSlug = other.getProjectSlug();
if (this$projectSlug == null ? other$projectSlug != null
: !this$projectSlug.equals(other$projectSlug))
return false;
final Object this$iterationSlug = this.getIterationSlug();
final Object other$iterationSlug = other.getIterationSlug();
if (this$iterationSlug == null ? other$iterationSlug != null
: !this$iterationSlug.equals(other$iterationSlug))
return false;
final Object this$docId = this.getDocId();
final Object other$docId = other.getDocId();
if (this$docId == null ? other$docId != null
: !this$docId.equals(other$docId))
return false;
return true;
}

@Override
public int hashCode() {
final int PRIME = 59;
int result = 1;
final Object $projectSlug = this.getProjectSlug();
result = result * PRIME
+ ($projectSlug == null ? 43 : $projectSlug.hashCode());
final Object $iterationSlug = this.getIterationSlug();
result = result * PRIME
+ ($iterationSlug == null ? 43 : $iterationSlug.hashCode());
final Object $docId = this.getDocId();
result = result * PRIME + ($docId == null ? 43 : $docId.hashCode());
return result;
}

public String getProjectSlug() {
return this.projectSlug;
}
Expand All @@ -233,5 +200,20 @@ public void setDocId(final String docId) {

private CopyTransProcessKey() {
}

@Override
public String id() {
return joinFields(KEY_NAME, projectSlug, iterationSlug, docId);
}

@Override
public CopyTransProcessKey from(String id) {
List<String> parts = parseId(id, KEY_NAME, 3);
CopyTransProcessKey key = new CopyTransProcessKey();
key.projectSlug = parts.get(0);
key.iterationSlug = parts.get(1);
key.docId = parts.get(2);
return key;
}
}
}
@@ -1,6 +1,8 @@
package org.zanata.action;

import java.io.Serializable;
import java.util.List;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import javax.enterprise.context.Dependent;
import javax.inject.Inject;
Expand Down Expand Up @@ -83,55 +85,28 @@ public boolean isCopyVersionRunning(String projectSlug,
/**
* Key used for copy version task
*
* @param projectSlug
* - target project identifier
* @param versionSlug
* - target version identifier
*/
public static final class CopyVersionKey implements Serializable {
public static final class CopyVersionKey implements
AsyncTaskHandleManager.AsyncTaskKey<CopyVersionKey> {
private static final String KEY_NAME = "copyVersion";
private static final long serialVersionUID = 3889349239078033373L;
// target project identifier
private final String projectSlug;
// target version identifier
private final String versionSlug;

/**
*
* @param projectSlug
* - target project identifier
* @param versionSlug
* - target version identifier
*/
public static CopyVersionKey getKey(String projectSlug,
String versionSlug) {
return new CopyVersionKey(projectSlug, versionSlug);
}

@Override
public boolean equals(final Object o) {
if (o == this)
return true;
if (!(o instanceof CopyVersionManager.CopyVersionKey))
return false;
final CopyVersionKey other = (CopyVersionKey) o;
final Object this$projectSlug = this.getProjectSlug();
final Object other$projectSlug = other.getProjectSlug();
if (this$projectSlug == null ? other$projectSlug != null
: !this$projectSlug.equals(other$projectSlug))
return false;
final Object this$versionSlug = this.getVersionSlug();
final Object other$versionSlug = other.getVersionSlug();
if (this$versionSlug == null ? other$versionSlug != null
: !this$versionSlug.equals(other$versionSlug))
return false;
return true;
}

@Override
public int hashCode() {
final int PRIME = 59;
int result = 1;
final Object $projectSlug = this.getProjectSlug();
result = result * PRIME
+ ($projectSlug == null ? 43 : $projectSlug.hashCode());
final Object $versionSlug = this.getVersionSlug();
result = result * PRIME
+ ($versionSlug == null ? 43 : $versionSlug.hashCode());
return result;
}

public String getProjectSlug() {
return this.projectSlug;
}
Expand All @@ -146,5 +121,16 @@ public CopyVersionKey(final String projectSlug,
this.projectSlug = projectSlug;
this.versionSlug = versionSlug;
}

@Override
public String id() {
return joinFields(KEY_NAME, projectSlug, versionSlug);
}

@Override
public CopyVersionKey from(String id) {
List<String> parts = parseId(id, KEY_NAME, 2);
return getKey(parts.get(0), parts.get(1));
}
}
}
@@ -1,10 +1,12 @@
package org.zanata.action;

import java.io.Serializable;
import java.util.List;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import javax.enterprise.context.Dependent;
import javax.inject.Inject;
import javax.inject.Named;

import org.zanata.async.AsyncTaskHandleManager;
import org.zanata.async.handle.MergeTranslationsTaskHandle;
import org.zanata.security.ZanataIdentity;
Expand Down Expand Up @@ -48,7 +50,9 @@ public class MergeTranslationsManager implements Serializable {
public void start(String sourceProjectSlug, String sourceVersionSlug,
String targetProjectSlug, String targetVersionSlug,
boolean useNewerTranslation) {
Key key = Key.getKey(targetProjectSlug, targetVersionSlug);
MergeVersionKey
key = MergeVersionKey
.getKey(targetProjectSlug, targetVersionSlug);
MergeTranslationsTaskHandle handle = new MergeTranslationsTaskHandle();
asyncTaskHandleManager.registerTaskHandle(handle, key);
mergeTranslationsServiceImpl.startMergeTranslations(sourceProjectSlug,
Expand Down Expand Up @@ -79,7 +83,7 @@ public void cancel(String projectSlug, String versionSlug) {
public MergeTranslationsTaskHandle getProcessHandle(String projectSlug,
String versionSlug) {
return (MergeTranslationsTaskHandle) asyncTaskHandleManager
.getHandleByKey(Key.getKey(projectSlug, versionSlug));
.getHandleByKey(MergeVersionKey.getKey(projectSlug, versionSlug));
}

public boolean isRunning(String projectSlug, String versionSlug) {
Expand All @@ -89,50 +93,21 @@ public boolean isRunning(String projectSlug, String versionSlug) {
}

/**
* Key used for copy version task
* Key used for merge version task
*/
public static final class Key implements Serializable {
public static final class MergeVersionKey implements
AsyncTaskHandleManager.AsyncTaskKey<MergeVersionKey> {
private static final long serialVersionUID = 1L;
private static final String KEY_NAME = "mergeVersion";
// target project identifier
private final String projectSlug;
// target version identifier
private final String versionSlug;

public static Key getKey(String projectSlug, String versionSlug) {
return new Key(projectSlug, versionSlug);
public static MergeVersionKey getKey(String projectSlug, String versionSlug) {
return new MergeVersionKey(projectSlug, versionSlug);
}

@Override
public boolean equals(final Object o) {
if (o == this)
return true;
if (!(o instanceof MergeTranslationsManager.Key))
return false;
final Key other = (Key) o;
final Object this$projectSlug = this.getProjectSlug();
final Object other$projectSlug = other.getProjectSlug();
if (this$projectSlug == null ? other$projectSlug != null
: !this$projectSlug.equals(other$projectSlug))
return false;
final Object this$versionSlug = this.getVersionSlug();
final Object other$versionSlug = other.getVersionSlug();
if (this$versionSlug == null ? other$versionSlug != null
: !this$versionSlug.equals(other$versionSlug))
return false;
return true;
}

@Override
public int hashCode() {
final int PRIME = 59;
int result = 1;
final Object $projectSlug = this.getProjectSlug();
result = result * PRIME
+ ($projectSlug == null ? 43 : $projectSlug.hashCode());
final Object $versionSlug = this.getVersionSlug();
result = result * PRIME
+ ($versionSlug == null ? 43 : $versionSlug.hashCode());
return result;
}

public String getProjectSlug() {
return this.projectSlug;
Expand All @@ -143,9 +118,20 @@ public String getVersionSlug() {
}

@java.beans.ConstructorProperties({ "projectSlug", "versionSlug" })
public Key(final String projectSlug, final String versionSlug) {
public MergeVersionKey(final String projectSlug, final String versionSlug) {
this.projectSlug = projectSlug;
this.versionSlug = versionSlug;
}

@Override
public String id() {
return joinFields(KEY_NAME, projectSlug, versionSlug);
}

@Override
public MergeVersionKey from(String id) {
List<String> parts = parseId(id, KEY_NAME, 2);
return getKey(parts.get(0), parts.get(1));
}
}
}
Expand Up @@ -89,7 +89,7 @@ public List<TransMemory> getAllTranslationMemories() {
}

public void sortTMList() {
Collections.sort(transMemoryList, tmComparator);
transMemoryList.sort(tmComparator);
}

public void clearTransMemory(final String transMemorySlug) {
Expand Down Expand Up @@ -198,7 +198,10 @@ public String cancel() {
* NB: Eventually this class might need to live outside if there are other
* services that need to control this process.
*/
private static class ClearTransMemoryProcessKey implements Serializable {
private static class ClearTransMemoryProcessKey implements
AsyncTaskHandleManager.AsyncTaskKey<ClearTransMemoryProcessKey> {
private static final String KEY_NAME = "ClearTMXKey";
private static final long serialVersionUID = 3472355792561903500L;
private String slug;

@java.beans.ConstructorProperties({ "slug" })
Expand All @@ -207,34 +210,14 @@ public ClearTransMemoryProcessKey(final String slug) {
}

@Override
public boolean equals(final Object o) {
if (o == this)
return true;
if (!(o instanceof TranslationMemoryAction.ClearTransMemoryProcessKey))
return false;
final ClearTransMemoryProcessKey other =
(ClearTransMemoryProcessKey) o;
if (!other.canEqual((Object) this))
return false;
final Object this$slug = this.slug;
final Object other$slug = other.slug;
if (this$slug == null ? other$slug != null
: !this$slug.equals(other$slug))
return false;
return true;
}

protected boolean canEqual(final Object other) {
return other instanceof TranslationMemoryAction.ClearTransMemoryProcessKey;
public String id() {
return joinFields(KEY_NAME, slug);
}

@Override
public int hashCode() {
final int PRIME = 59;
int result = 1;
final Object $slug = this.slug;
result = result * PRIME + ($slug == null ? 43 : $slug.hashCode());
return result;
public ClearTransMemoryProcessKey from(String id) {
List<String> parts = parseId(id, KEY_NAME, 1);
return new ClearTransMemoryProcessKey(parts.get(0));
}
}

Expand Down

0 comments on commit 978639f

Please sign in to comment.