Skip to content

Commit

Permalink
ref torakiki#431: a litle more readable
Browse files Browse the repository at this point in the history
  • Loading branch information
torakiki committed Nov 9, 2020
1 parent a0c3050 commit 0cc02f6
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class PreferencesUsageDataStore {
static final String USAGE_PATH = "/org/pdfsam/modules/usage";
static final String MODULE_USAGE_KEY = "module.usage";
static final String TASKS_EXECUTED_KEY = "tasks.executed";
private JSON jackson = new JSON().without(Feature.USE_FIELDS);

public PreferencesUsageDataStore() {
eventStudio().addAnnotatedListeners(this);
Expand All @@ -56,11 +57,9 @@ public void incrementUsageFor(String moduleId) {
String json = node.get(MODULE_USAGE_KEY, "");
try {
if (isNotBlank(json)) {
node.put(MODULE_USAGE_KEY, JSON.std
.asString(JSON.std.without(Feature.USE_FIELDS).beanFrom(ModuleUsage.class, json).inc()));
node.put(MODULE_USAGE_KEY, jackson.asString(jackson.beanFrom(ModuleUsage.class, json).inc()));
} else {
node.put(MODULE_USAGE_KEY,
JSON.std.without(Feature.USE_FIELDS).asString(ModuleUsage.fistUsage(moduleId)));
node.put(MODULE_USAGE_KEY, jackson.asString(ModuleUsage.fistUsage(moduleId)));
}
LOG.trace("Usage incremented for module {}", moduleId);
} catch (IOException e) {
Expand All @@ -77,7 +76,7 @@ public List<ModuleUsage> getUsages() {
List<String> jsons = Arrays.stream(prefs.childrenNames()).parallel().map(name -> prefs.node(name))
.map(node -> node.get(MODULE_USAGE_KEY, "")).filter(json -> isNotBlank(json)).collect(toList());
for (String json : jsons) {
retList.add(JSON.std.without(Feature.USE_FIELDS).beanFrom(ModuleUsage.class, json));
retList.add(jackson.beanFrom(ModuleUsage.class, json));
}
} catch (BackingStoreException | IOException e) {
LOG.error("Unable to get modules usage statistics", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public class DefaultNewsService implements NewsService {
private static final String LATEST_NEWS_ID = "latest.news.id";
private static final String LATEST_IMPORTANT_NEWS_ID = "latest.important.news.id";
private Pdfsam pdfsam;
private JSON jackson = new JSON().without(Feature.USE_FIELDS).with(Feature.READ_ONLY, true);

@Inject
DefaultNewsService(Pdfsam pdfsam) {
Expand All @@ -61,7 +62,7 @@ public class DefaultNewsService implements NewsService {
@Override
public List<NewsData> getLatestNews() {
try {
return JSON.std.without(Feature.USE_FIELDS).with(Feature.READ_ONLY, true)
return jackson
.listOfFrom(NewsData.class,
urlToStream(new URL(pdfsam.property(ConfigurableProperty.NEWS_URL))));
} catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public class DefaultPremiumModulesService implements PremiumModulesService {

private static final Logger LOG = LoggerFactory.getLogger(DefaultPremiumModulesService.class);
private Pdfsam pdfsam;
private JSON jackson = new JSON().without(Feature.USE_FIELDS).with(Feature.READ_ONLY, true);

@Inject
DefaultPremiumModulesService(Pdfsam pdfsam) {
Expand All @@ -55,7 +56,7 @@ public class DefaultPremiumModulesService implements PremiumModulesService {
@Override
public List<PremiumModule> getPremiumModules() {
try {
return JSON.std.without(Feature.USE_FIELDS).with(Feature.READ_ONLY, true).listOfFrom(
return jackson.listOfFrom(
PremiumModule.class,
urlToStream(new URL(pdfsam.property(ConfigurableProperty.PREMIUM_MODULES_URL))));
} catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,13 @@ class DefaultStageService implements StageService {
private static final Logger LOG = LoggerFactory.getLogger(DefaultStageService.class);
static final String STAGE_PATH = "/org/pdfsam/stage";
static final String STAGE_STATUS_KEY = "stage.status";
private JSON jackson = new JSON().without(Feature.USE_FIELDS);

@Override
public void save(StageStatus status) {
Preferences node = Preferences.userRoot().node(STAGE_PATH);
try {
node.put(STAGE_STATUS_KEY, JSON.std.without(Feature.USE_FIELDS).asString(status));
node.put(STAGE_STATUS_KEY, jackson.asString(status));
LOG.trace("Stage status saved {}", status);
} catch (IOException e) {
LOG.error("Unable to save Stage status", e);
Expand All @@ -59,7 +60,7 @@ public StageStatus getLatestStatus() {
try {
String statusString = node.get(STAGE_STATUS_KEY, "");
if (isNotBlank(statusString)) {
return JSON.std.without(Feature.USE_FIELDS).beanFrom(StageStatus.class, statusString);
return jackson.beanFrom(StageStatus.class, statusString);
}
} catch (IOException e) {
LOG.error("Unable to get latest stage status", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@
*/
class JsonWorkspaceService implements WorkspaceService {
private static final Logger LOG = LoggerFactory.getLogger(JsonWorkspaceService.class);
private JSON jackson = new JSON().without(Feature.USE_FIELDS).with(JSON.Feature.PRETTY_PRINT_OUTPUT)
.without(JSON.Feature.WRITE_NULL_PROPERTIES);

@Override
public void saveWorkspace(Map<String, Map<String, String>> data, File destination) {
requireNotNullArg(destination, "Destination file cannot be null");
LOG.debug(DefaultI18nContext.getInstance().i18n("Saving workspace data to {0}", destination.getAbsolutePath()));
try {
JSON.std.without(Feature.USE_FIELDS).with(JSON.Feature.PRETTY_PRINT_OUTPUT).without(
JSON.Feature.WRITE_NULL_PROPERTIES)
.write(data, destination);
jackson.write(data, destination);
LOG.info(DefaultI18nContext.getInstance().i18n("Workspace saved"));
} catch (Exception e) {
// make it unchecked
Expand All @@ -62,7 +62,7 @@ public Map<String, Map<String, String>> loadWorkspace(File workspace) {
requireNotNullArg(workspace, "Workspace file cannot be null");
Map<String, Map<String, String>> data = Collections.emptyMap();
try (FileInputStream stream = new FileInputStream(workspace)) {
data = (Map) JSON.std.without(Feature.USE_FIELDS).mapFrom(stream);
data = (Map) jackson.mapFrom(stream);
} catch (Exception e) {
// make it unchecked
throw new RuntimeException(e);
Expand Down

0 comments on commit 0cc02f6

Please sign in to comment.