Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update FieldAdapter with Nullable/NonNull annotations. Fix code in im… #649

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public final class TaskFieldAdapters
* Adapter for the status of a task.
*/
public final static IntegerFieldAdapter STATUS = (IntegerFieldAdapter) new IntegerFieldAdapter(Tasks.STATUS, Tasks.STATUS_NEEDS_ACTION)
.addContraint(new AdjustPercentComplete(PERCENT_COMPLETE));
.addConstraint(new AdjustPercentComplete(PERCENT_COMPLETE));

/**
* Adapter for the priority value of a task.
Expand Down Expand Up @@ -108,7 +108,7 @@ public final class TaskFieldAdapters
* Adapter for the checklist of a task.
*/
public final static ChecklistFieldAdapter CHECKLIST = (ChecklistFieldAdapter) new ChecklistFieldAdapter(Tasks.DESCRIPTION)
.addContraint(new ChecklistConstraint(STATUS, PERCENT_COMPLETE));
.addConstraint(new ChecklistConstraint(STATUS, PERCENT_COMPLETE));

/**
* Private adapter for the start date of a task. We need this to reference DTSTART from DUE.
Expand All @@ -119,12 +119,12 @@ public final class TaskFieldAdapters
/**
* Adapter for the due date of a task.
*/
public final static FieldAdapter<Time> DUE = new CustomizedDefaultFieldAdapter<Time>(_DUE, new DefaultAfter(_DTSTART)).addContraint(new After(_DTSTART));
public final static FieldAdapter<Time> DUE = new CustomizedDefaultFieldAdapter<Time>(_DUE, new DefaultAfter(_DTSTART)).addConstraint(new After(_DTSTART));

/**
* Adapter for the start date of a task.
*/
public final static FieldAdapter<Time> DTSTART = new CustomizedDefaultFieldAdapter<Time>(_DTSTART, new DefaultBefore(DUE)).addContraint(
public final static FieldAdapter<Time> DTSTART = new CustomizedDefaultFieldAdapter<Time>(_DTSTART, new DefaultBefore(DUE)).addConstraint(
new BeforeOrShiftTime(DUE));

/**
Expand Down
4 changes: 2 additions & 2 deletions opentasks/src/main/java/org/dmfs/tasks/model/XmlModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -212,11 +212,11 @@ public XmlModel finish(ElementDescriptor<XmlModel> descriptor, XmlModel object,
// add UpdateAllDay constraint of due or start fields are missing to keep the values in sync with the allday flag
if (!state.hasDue)
{
((FieldAdapter<Boolean>) state.alldayDescriptor.getFieldAdapter()).addContraint(new UpdateAllDay(TaskFieldAdapters.DUE));
((FieldAdapter<Boolean>) state.alldayDescriptor.getFieldAdapter()).addConstraint(new UpdateAllDay(TaskFieldAdapters.DUE));
}
if (!state.hasStart)
{
((FieldAdapter<Boolean>) state.alldayDescriptor.getFieldAdapter()).addContraint(new UpdateAllDay(TaskFieldAdapters.DTSTART));
((FieldAdapter<Boolean>) state.alldayDescriptor.getFieldAdapter()).addConstraint(new UpdateAllDay(TaskFieldAdapters.DTSTART));
}
}
return object;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,12 @@

import android.content.ContentValues;
import android.database.Cursor;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import org.dmfs.tasks.model.ContentSet;
import org.dmfs.tasks.model.OnContentChangeListener;
import org.dmfs.tasks.utils.BooleanBinaryLong;


/**
Expand Down Expand Up @@ -79,7 +82,7 @@ public BooleanFieldAdapter(String fieldName, Boolean defaultValue)


@Override
public Boolean get(ContentSet values)
public Boolean get(@NonNull ContentSet values)
{
Integer value = values.getAsInteger(mFieldName);

Expand All @@ -88,7 +91,7 @@ public Boolean get(ContentSet values)


@Override
public Boolean get(Cursor cursor)
public Boolean get(@NonNull Cursor cursor)
{
int columnIdx = cursor.getColumnIndex(mFieldName);
if (columnIdx < 0)
Expand All @@ -100,35 +103,35 @@ public Boolean get(Cursor cursor)


@Override
public Boolean getDefault(ContentSet values)
public Boolean getDefault(@NonNull ContentSet values)
{
return mDefaultValue;
}


@Override
public void set(ContentSet values, Boolean value)
public void set(@NonNull ContentSet values, @Nullable Boolean value)
{
values.put(mFieldName, value ? 1 : 0);
values.put(mFieldName, new BooleanBinaryLong(value).value());
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why long?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason why I started to use Long instead of String for 0/1 booleans is that I saw that in SQLite these booleans are actually stored as number. I just looked up again:
https://www.sqlite.org/datatype3.html#boolean_datatype
I see integer can be 8 bytes there, so java long would cover it, but for this 0/1, java integer could fit as well, I suppose.

This also relates to the type I've introduce in the other pull request:

public interface DateTimeFields
{
/**
* The timestamp. ({@code null} if it's empty)
*/
@Nullable
Long timestamp();
/**
* Time zone id. ({@code null} if it's empty)
*/
@Nullable
String timeZoneId();
/**
* All-day flag of the date-time. 1 for true, 0 for false.
* <p>
* ({@code null} if it's empty, which is always interpreted as 0-false)
*/
@Nullable
Long isAllDay();
}

So the all-day flag is represented as Long here as well, as its raw value.

So in order to keep this consistent, I chose Long here, too. Although, I remember that some container types only store everything in String (Which type was that? I forgot. ) which may mean a double conversion. So it's not that obvious what's best.
But the idea again was to use the raw type and keep it consistent for simplicity.

What do you think?
We could certainly change to Integer at least.

Copy link
Contributor Author

@lemonboston lemonboston Jan 26, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or we could even use Short. (I've never used that type before..:) )

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Short is not supported by all containers, so that's not good. Integer is supported.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've changed this to use Integer.

}


@Override
public void set(ContentValues values, Boolean value)
public void set(@NonNull ContentValues values, @Nullable Boolean value)
{
values.put(mFieldName, value ? 1 : 0);
values.put(mFieldName, new BooleanBinaryLong(value).value());
}


@Override
public void registerListener(ContentSet values, OnContentChangeListener listener, boolean initalNotification)
public void registerListener(@NonNull ContentSet values, @NonNull OnContentChangeListener listener, boolean initalNotification)
{
values.addOnChangeListener(listener, mFieldName, initalNotification);
}


@Override
public void unregisterListener(ContentSet values, OnContentChangeListener listener)
public void unregisterListener(@NonNull ContentSet values, @NonNull OnContentChangeListener listener)
{
values.removeOnChangeListener(listener, mFieldName);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

import android.content.ContentValues;
import android.database.Cursor;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import org.dmfs.tasks.model.CheckListItem;
import org.dmfs.tasks.model.ContentSet;
Expand Down Expand Up @@ -77,16 +79,17 @@ public ChecklistFieldAdapter(String fieldName, List<CheckListItem> defaultValue)
}


@Nullable
@Override
public List<CheckListItem> get(ContentSet values)
public List<CheckListItem> get(@NonNull ContentSet values)
{
// return the check list
return extractCheckList(values.getAsString(mFieldName));
}


@Override
public List<CheckListItem> get(Cursor cursor)
public List<CheckListItem> get(@NonNull Cursor cursor)
{
int columnIdx = cursor.getColumnIndex(mFieldName);
if (columnIdx < 0)
Expand All @@ -98,14 +101,14 @@ public List<CheckListItem> get(Cursor cursor)


@Override
public List<CheckListItem> getDefault(ContentSet values)
public List<CheckListItem> getDefault(@NonNull ContentSet values)
{
return mDefaultValue;
}


@Override
public void set(ContentSet values, List<CheckListItem> value)
public void set(@NonNull ContentSet values, @Nullable List<CheckListItem> value)
{
String oldDescription = DescriptionStringFieldAdapter.extractDescription(values.getAsString(mFieldName));
if (value != null && !value.isEmpty())
Expand All @@ -130,7 +133,7 @@ public void set(ContentSet values, List<CheckListItem> value)


@Override
public void set(ContentValues values, List<CheckListItem> value)
public void set(@NonNull ContentValues values, @Nullable List<CheckListItem> value)
{
String oldDescription = DescriptionStringFieldAdapter.extractDescription(values.getAsString(mFieldName));
if (value != null && !value.isEmpty())
Expand All @@ -156,14 +159,14 @@ public void set(ContentValues values, List<CheckListItem> value)


@Override
public void registerListener(ContentSet values, OnContentChangeListener listener, boolean initalNotification)
public void registerListener(@NonNull ContentSet values, @NonNull OnContentChangeListener listener, boolean initalNotification)
{
values.addOnChangeListener(listener, mFieldName, initalNotification);
}


@Override
public void unregisterListener(ContentSet values, OnContentChangeListener listener)
public void unregisterListener(@NonNull ContentSet values, @NonNull OnContentChangeListener listener)
{
values.removeOnChangeListener(listener, mFieldName);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@

import android.database.Cursor;
import android.graphics.Color;
import android.support.annotation.ColorInt;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import org.dmfs.tasks.model.ContentSet;

Expand Down Expand Up @@ -88,28 +91,34 @@ public ColorFieldAdapter(String fieldName, Integer defaultValue, float darkenThr


@Override
public Integer get(ContentSet values)
public Integer get(@NonNull ContentSet values)
{
return darkenColor(super.get(values), mDarkenThreshold);
}


@Override
public Integer get(Cursor cursor)
public Integer get(@NonNull Cursor cursor)
{
return darkenColor(super.get(cursor), mDarkenThreshold);
}


@Override
public Integer getDefault(ContentSet values)
public Integer getDefault(@NonNull ContentSet values)
{
return darkenColor(super.getDefault(values), mDarkenThreshold);
}


private static int darkenColor(int color, float maxLuminance)
@ColorInt
private static Integer darkenColor(@Nullable Integer color, float maxLuminance)
{
if (color == null)
{
return null;
}

float[] hsv = new float[3];
Color.colorToHSV(color, hsv);
hsv[2] = hsv[2] * hsv[2] * hsv[2] * hsv[2] * hsv[2] * (maxLuminance - 1) + hsv[2];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

import android.content.ContentValues;
import android.database.Cursor;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import org.dmfs.tasks.model.ContentSet;
import org.dmfs.tasks.model.OnContentChangeListener;
Expand Down Expand Up @@ -59,15 +61,17 @@ public CustomizedDefaultFieldAdapter(FieldAdapter<Type> fieldAdapter, Default<Ty
}


@Nullable
@Override
public Type get(ContentSet values)
public Type get(@NonNull ContentSet values)
{
return mFieldAdapter.get(values);
}


@Nullable
@Override
public Type get(Cursor cursor)
public Type get(@NonNull Cursor cursor)
{
return mFieldAdapter.get(cursor);
}
Expand All @@ -81,37 +85,38 @@ public Type get(Cursor cursor)
*
* @return A default Value
*/
@Nullable
@Override
public Type getDefault(ContentSet values)
public Type getDefault(@NonNull ContentSet values)
{
Type defaultValue = mFieldAdapter.getDefault(values);
return mDefault.getCustomDefault(values, defaultValue);
}


@Override
public void set(ContentSet values, Type value)
public void set(@NonNull ContentSet values, @Nullable Type value)
{
mFieldAdapter.set(values, value);
}


@Override
public void set(ContentValues values, Type value)
public void set(@NonNull ContentValues values, @Nullable Type value)
{
mFieldAdapter.set(values, value);
}


@Override
public void registerListener(ContentSet values, OnContentChangeListener listener, boolean initialNotification)
public void registerListener(@NonNull ContentSet values, @NonNull OnContentChangeListener listener, boolean initialNotification)
{
mFieldAdapter.registerListener(values, listener, initialNotification);
}


@Override
public void unregisterListener(ContentSet values, OnContentChangeListener listener)
public void unregisterListener(@NonNull ContentSet values, @NonNull OnContentChangeListener listener)
{
mFieldAdapter.unregisterListener(values, listener);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
package org.dmfs.tasks.model.adapters;

import android.database.Cursor;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import org.dmfs.tasks.model.ContentSet;

Expand Down Expand Up @@ -56,21 +58,21 @@ public DescriptionStringFieldAdapter(String fieldName, String defaultValue)


@Override
public String get(ContentSet values)
public String get(@NonNull ContentSet values)
{
return extractDescription(super.get(values));
}


@Override
public String get(Cursor cursor)
public String get(@NonNull Cursor cursor)
{
return extractDescription(super.get(cursor));
}


@Override
public void set(ContentSet values, String value)
public void set(@NonNull ContentSet values, @Nullable String value)
{
String oldValue = super.get(values);
if (oldValue != null && oldValue.length() > 0)
Expand Down
Loading