Skip to content

Commit

Permalink
Add some @nullable @nonnull to SharedList.java
Browse files Browse the repository at this point in the history
  • Loading branch information
AChep committed Aug 21, 2014
1 parent 10efd22 commit d9f57f5
Showing 1 changed file with 22 additions and 13 deletions.
35 changes: 22 additions & 13 deletions project/AcDisplay/src/main/java/com/achep/acdisplay/SharedList.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

import android.content.Context;
import android.content.SharedPreferences;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;

import java.util.ArrayList;
Expand Down Expand Up @@ -69,14 +71,14 @@ public interface OnSharedListChangedListener<V> {
* @param objectOld old object from the list
* @param diff the difference between old and new objects (provided by {@link SharedList.Comparator})
*/
public void onPut(V objectNew, V objectOld, int diff);
public void onPut(@NonNull V objectNew, @Nullable V objectOld, int diff);

/**
* Called on object removed from the list.
*
* @param objectRemoved removed object from the list
*/
public void onRemoved(V objectRemoved);
public void onRemoved(@NonNull V objectRemoved);
}

/**
Expand All @@ -92,7 +94,7 @@ public static abstract class Comparator<V> {
*
* @return The difference between old and new objects.
*/
public abstract int compare(V object, V old);
public abstract int compare(@NonNull V object, @Nullable V old);
}

/**
Expand All @@ -112,15 +114,17 @@ public static abstract class Saver<V> {
* @param position position of given object in list
* @see #get(android.content.SharedPreferences, int)
*/
public abstract SharedPreferences.Editor put(V object, SharedPreferences.Editor editor, int position);
public abstract SharedPreferences.Editor put(@NonNull V object,
@NonNull SharedPreferences.Editor editor,
int position);

/**
* Restores previously save Object from shared preferences.
*
* @param position position of given object in list
* @see #put(Object, android.content.SharedPreferences.Editor, int)
*/
public abstract V get(SharedPreferences prefs, int position);
public abstract V get(@NonNull SharedPreferences prefs, int position);

}

Expand All @@ -130,7 +134,7 @@ public static abstract class Saver<V> {
* @see #unregisterListener(SharedList.OnSharedListChangedListener)
* @see SharedList.OnSharedListChangedListener
*/
public void registerListener(OnSharedListChangedListener<V> listener) {
public void registerListener(@NonNull OnSharedListChangedListener<V> listener) {
mListeners.add(listener);
}

Expand All @@ -140,13 +144,13 @@ public void registerListener(OnSharedListChangedListener<V> listener) {
* @see #registerListener(SharedList.OnSharedListChangedListener)
* @see SharedList.OnSharedListChangedListener
*/
public void unregisterListener(OnSharedListChangedListener<V> listener) {
public void unregisterListener(@NonNull OnSharedListChangedListener<V> listener) {
mListeners.remove(listener);
}

protected SharedList() { /* You must call #init(Context) later! */ }

protected SharedList(Context context) {
protected SharedList(@NonNull Context context) {
init(context);
}

Expand Down Expand Up @@ -177,7 +181,7 @@ protected void init(Context context) {
}
}

private SharedPreferences getSharedPreferences(Context context) {
private SharedPreferences getSharedPreferences(@NonNull Context context) {
return context.getSharedPreferences(getPreferencesFileName(), Context.MODE_PRIVATE);
}

Expand All @@ -200,6 +204,7 @@ private SharedPreferences getSharedPreferences(Context context) {
* @see #put(android.content.Context, Object, OnSharedListChangedListener)
* @see #getComparator()
*/
@Nullable
protected Comparator<V> onCreateComparator() {
return null;
}
Expand All @@ -208,6 +213,7 @@ protected Comparator<V> onCreateComparator() {
* @return Previously created comparator.
* @see #onCreateComparator()
*/
@Nullable
public Comparator<V> getComparator() {
return mComparator;
}
Expand All @@ -216,11 +222,11 @@ protected boolean isOverwriteAllowed(V object) {
return false;
}

public void remove(Context context, V object) {
public void remove(@NonNull Context context, V object) {
remove(context, object, null);
}

public void remove(Context context, V object, OnSharedListChangedListener l) {
public void remove(@NonNull Context context, V object, @Nullable OnSharedListChangedListener l) {
if (!mList.containsKey(object)) {
Log.w(TAG, "Tried to remove non-existing object from the list.");
return;
Expand All @@ -246,11 +252,13 @@ public void remove(Context context, V object, OnSharedListChangedListener l) {
notifyOnRemoved(objectRemoved, l);
}

public V put(Context context, V object) {
@Nullable
public V put(@NonNull Context context, V object) {
return put(context, object, null);
}

public V put(Context context, V object, OnSharedListChangedListener l) {
@Nullable
public V put(@NonNull Context context, V object, @Nullable OnSharedListChangedListener l) {
// Check for correct arguments.
if (object == null) {
if (Build.DEBUG) {
Expand Down Expand Up @@ -352,6 +360,7 @@ public boolean contains(V object) {
return mList.containsKey(object);
}

@NonNull
public Set<V> valuesSet() {
return mList.keySet();
}
Expand Down

0 comments on commit d9f57f5

Please sign in to comment.