Skip to content

Commit

Permalink
Refactor to use self()
Browse files Browse the repository at this point in the history
  • Loading branch information
Eldelshell committed Sep 18, 2014
1 parent 3d2116c commit b4c9467
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ private PreferencesController() {
? Preferences.userRoot().node("jobhunter-dev")
: Preferences.userRoot().node("jobhunter");
}

public static void init() {

Boolean isFirstTime = Boolean.TRUE;
Expand Down
16 changes: 10 additions & 6 deletions jobhunter/src/main/java/jobhunter/persistence/Persistence.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ private Persistence() {
xstream.registerConverter(new LocalDateConverter());
}

private static Persistence self() {
return _INSTANCE;
}

@SuppressWarnings("unchecked")
static <T> List<T> cast(Object obj) {
return (List<T>)obj;
Expand Down Expand Up @@ -174,27 +178,27 @@ private Boolean wasModified(final File file) {
}

public static void save(final File file) {
_INSTANCE._save(file, false);
self()._save(file, false);
}

public static void rewrite(final File file) {
_INSTANCE._save(file, true);
self()._save(file, true);
}

public static Optional<Profile> readProfile(final File file) {
return _INSTANCE._readProfile(file);
return self()._readProfile(file);
}

public static Optional<List<Subscription>> readSubscriptions(final File file) {
return _INSTANCE._readSubscriptions(file);
return self()._readSubscriptions(file);
}

public static String debugProfile() {
return _INSTANCE.xstream.toXML(ProfileRepository.getProfile());
return self().xstream.toXML(ProfileRepository.getProfile());
}

public static String debugSubscriptions() {
return _INSTANCE.xstream.toXML(SubscriptionRepository.getSubscriptions());
return self().xstream.toXML(SubscriptionRepository.getSubscriptions());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ public enum ProfileRepository {

private static final Logger l = LoggerFactory.getLogger(ProfileRepository.class);

@Deprecated
public static ProfileRepository instanceOf() {
private static ProfileRepository self() {
return _INSTANCE;
}

Expand Down Expand Up @@ -105,57 +104,57 @@ private void fireEvent() {
// Static methods

public static void save(final Job job){
_INSTANCE.saveJob(job);
self().saveJob(job);
}

public static void delete(final Job job){
_INSTANCE.deleteJob(job);
self().deleteJob(job);
}

public static Optional<Job> getJob(final ObjectId id) {
return _INSTANCE.current.getJobs()
return self().current.getJobs()
.stream()
.filter(j -> j.getId().equals(id))
.findFirst();
}

public static List<Job> getActiveJobs() {
return _INSTANCE.current.getJobs()
return self().current.getJobs()
.stream()
.filter(j -> j.getActive())
.sorted()
.collect(Collectors.toList());
}

public static List<Job> getAllJobs() {
return _INSTANCE.current.getJobs()
return self().current.getJobs()
.stream()
.sorted()
.collect(Collectors.toList());
}

public static List<Job> getJobsByDate(Boolean all) {
return _INSTANCE.getJobsBy(all, new Order.CreatedComparator(Order.DESCENDING));
return self().getJobsBy(all, new Order.CreatedComparator(Order.DESCENDING));
}

public static List<Job> getJobsByRating(Boolean all) {
return _INSTANCE.getJobsBy(all, new Order.RatingComparator(Order.DESCENDING));
return self().getJobsBy(all, new Order.RatingComparator(Order.DESCENDING));
}

public static List<Job> getJobsByActivity(Boolean all) {
return _INSTANCE.getJobsBy(all, new Order.ActivityComparator(Order.DESCENDING));
return self().getJobsBy(all, new Order.ActivityComparator(Order.DESCENDING));
}

public static List<Job> getJobsByStatus(Boolean all) {
return _INSTANCE.getJobsBy(all, new Order.StatusComparator(Order.DESCENDING));
return self().getJobsBy(all, new Order.StatusComparator(Order.DESCENDING));
}

public static Profile getProfile() {
return _INSTANCE._getProfile();
return self()._getProfile();
}

public static void clear() {
_INSTANCE._clear();
self()._clear();
}

public static Set<String> getAutocompletePositions() {
Expand All @@ -171,19 +170,19 @@ public static Set<String> getAutocompleteCompanies() {
}

public static void load(final File file) {
_INSTANCE._load(file);
self()._load(file);
}

public static ProfileRepositoryListener getListener() {
return _INSTANCE.listener;
return self().listener;
}

public static void setListener(ProfileRepositoryListener listener) {
_INSTANCE.listener = listener;
self().listener = listener;
}

public static void setProfile(Profile profile) {
_INSTANCE.current = profile;
self().current = profile;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,19 @@ public enum SubscriptionRepository {
private SubscriptionRepositoryListener listener;

private List<Subscription> subscriptions;

private static SubscriptionRepository self() {
return _INSTANCE;
}

public static List<Subscription> getSubscriptions() {
if(_INSTANCE.subscriptions == null)
_INSTANCE.subscriptions = new ArrayList<>();
return _INSTANCE.subscriptions;
if(self().subscriptions == null)
self().subscriptions = new ArrayList<>();
return self().subscriptions;
}

public static void setSubscriptions(List<Subscription> subscriptions) {
_INSTANCE.subscriptions = subscriptions;
self().subscriptions = subscriptions;
}

public static void add(final Subscription subs){
Expand All @@ -64,7 +68,7 @@ public static void add(final Subscription subs){
public static void delete(final Subscription subs){
l.debug("Removing subscription");
getSubscriptions().remove(subs);
_INSTANCE.fireEvent();
self().fireEvent();
ApplicationState.changesPending(true);
}

Expand All @@ -91,13 +95,13 @@ public static Optional<Subscription> findByItem(final SubscriptionItem item) {

public static void load(final File file) {
Optional<List<Subscription>> profile = Persistence.readSubscriptions(file);
_INSTANCE.subscriptions = profile.orElse(new ArrayList<>());
_INSTANCE.fireEvent();
self().subscriptions = profile.orElse(new ArrayList<>());
self().fireEvent();
}

public static void clear() {
_INSTANCE.subscriptions = new ArrayList<>();
_INSTANCE.fireEvent();
self().subscriptions = new ArrayList<>();
self().fireEvent();
}

private void fireEvent() {
Expand All @@ -106,12 +110,12 @@ private void fireEvent() {
}

public static SubscriptionRepositoryListener getListener() {
return _INSTANCE.listener;
return self().listener;
}

public static SubscriptionRepository setListener(SubscriptionRepositoryListener listener) {
_INSTANCE.listener = listener;
return _INSTANCE;
self().listener = listener;
return self();
}

}

0 comments on commit b4c9467

Please sign in to comment.