11 | * A fuzzing configuration is a set of ranges which are mapped to 12 | * internationalized strings (via String resources). Each of these ranges 13 | * represents a special human understanding of a distance of time or duration. 14 | * 15 | * @author ma� 16 | */ 17 | public interface FuzzingConfiguration { 18 | // ~ Methods 19 | // ---------------------------------------------------------------- 20 | 21 | /** 22 | * This method returns an array of range objects. These are used to map 23 | * ranges of relative distances of time to a string resource id. 24 | * 25 | * @param strenght 26 | * the fuzzing strength defining the granularity of ranges to 27 | * return. 28 | * 29 | * @return an array of ranges for distances of time. 30 | */ 31 | Range[] getDistanceRanges(FuzzingStrength strenght); 32 | 33 | /** 34 | * This method returns an array of range objects. These are used to map 35 | * ranges of relative durations of time to a string resource id. 36 | * 37 | * @param strenght 38 | * the fuzzing strength defining the granularity of ranges to 39 | * return. 40 | * 41 | * @return an array of ranges for duration of time. 42 | */ 43 | Range[] getDurationRanges(FuzzingStrength strenght); 44 | 45 | /** 46 | * This method returns an array of range objects. These are used to map 47 | * ranges of hours to a string resource id. 48 | * 49 | * @param strenght 50 | * the fuzzing strength defining the granularity of ranges to 51 | * return. 52 | * 53 | * @return an array of ranges for duration of time. 54 | */ 55 | Range[] getHourRanges(FuzzingStrength strenght); 56 | 57 | /** 58 | * Get a fuzzy string with a given range 59 | * 60 | * @param range 61 | * @param context 62 | * android context used to lookup resources 63 | * @param params 64 | * 65 | * @return 66 | */ 67 | String getFuzzyString(Range range, Context context, Object... params); 68 | 69 | /** 70 | * Get a fuzzy string with a given resource Id 71 | * 72 | * @param resId 73 | * Resource Id of the string to use 74 | * @param context 75 | * android context used to lookup resources 76 | * @param params 77 | * 78 | * @return 79 | */ 80 | String getFuzzyString(int resId, Context context, Object... params); 81 | } 82 | -------------------------------------------------------------------------------- /fuzzydateandroid-lib/src/com/fuzzydate/android/FuzzingStrength.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/scottyab/FuzzyDateAndroid/b7955bb56f2efc2b6e33888c20f1348a8213d2ee/fuzzydateandroid-lib/src/com/fuzzydate/android/FuzzingStrength.java -------------------------------------------------------------------------------- /fuzzydateandroid-lib/src/com/fuzzydate/android/FuzzyDateFormat.java: -------------------------------------------------------------------------------- 1 | package com.fuzzydate.android; 2 | 3 | import android.content.Context; 4 | 5 | import com.fuzzydate.android.impl.DefaultFuzzingConfiguration; 6 | import com.fuzzydate.android.impl.FuzzyDateFormatterImpl; 7 | 8 | /** 9 | * FuzzyDateFormat is a factory for creating instances of fuzzy time/date 10 | * formatters. These formatters format time/date, distances of time and 11 | * durations to internationalized strings. 12 | * 13 | * @author amaasch 14 | */ 15 | public final class FuzzyDateFormat { 16 | /** 17 | * Creates a fuzzy date formatter instance with the static default 18 | * configuration. 19 | * 20 | * @return a fuzzy date formatter instance. 21 | */ 22 | public static final FuzzyDateFormatter getInstance(final Context context) { 23 | return new FuzzyDateFormatterImpl( 24 | DefaultFuzzingConfiguration.getInstance(), context); 25 | } 26 | 27 | /** 28 | * Creates a fuzzy date formatter instance with a given configuration. 29 | * 30 | * @param config 31 | * the configuration for the date formatter. 32 | * 33 | * @return a fuzzy date formatter instance. 34 | */ 35 | public static final FuzzyDateFormatter getInstance( 36 | final FuzzingConfiguration config, final Context context) { 37 | return new FuzzyDateFormatterImpl(config, context); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /fuzzydateandroid-lib/src/com/fuzzydate/android/FuzzyDateFormatter.java: -------------------------------------------------------------------------------- 1 | package com.fuzzydate.android; 2 | 3 | import java.util.Date; 4 | 5 | /** 6 | * Formatter for human readable dates, distances of time and durations. 7 | * 8 | *
9 | * Use the {@link #format(Date)} methods to create relative strings like 10 | * "today", "last friday". These dates are used to tell the a user when an event 11 | * has happened or will happen. The string will be built relative to the current 12 | * time/date. 13 | * 14 | *
15 | * Use the {@link #formatDistance(Date)} methods to create strings for distances 16 | * of time such as "in 5 days", "a month ago". This format is used to tell the 17 | * user how much time has passed since an event or how much time is left before 18 | * something happens. These strings will also be built relative to the current 19 | * date/time. 20 | * 21 | *
22 | * Use the {@link #formatDuration(long)} methods to create strings representing
23 | * the time between two events, like "5 minutes", "two centuries". These strings
24 | * are used for durations of processes. Calling this method with a single date
25 | * will calculate the duration from this date to the current time.
26 | *
27 | *
28 | * @author amaasch
29 | */
30 | public interface FuzzyDateFormatter {
31 |
32 | /**
33 | * Creates a string representing a point in time in reference to the current
34 | * time/date. Use this method to tell the a user when an event has happened
35 | * or will happen. This method uses the strings resources to format the
36 | * string.
37 | *
38 | * @param date
39 | * the date to be formatted as fuzzy string.
40 | *
41 | * @return a fuzzy string representing a point in time.
42 | */
43 | String format(Date date);
44 |
45 | /**
46 | * Creates a string representing a distance to a given date. Use this method
47 | * to tell the user how much time has passed since an event or how much time
48 | * is left before something happens. This method uses the strings resources
49 | * to format the string.
50 | *
51 | * @param date
52 | * the relative date to which the relative distance will be
53 | * formatted as fuzzy string.
54 | *
55 | * @return a fuzzy string representing the distance from the current time.
56 | */
57 | String formatDistance(Date date);
58 |
59 | /**
60 | * Creates a string representing a duration. Use this method to tell the
61 | * user how much time has passes between two events. This method uses the
62 | * strings resources to format the string.
63 | *
64 | * @param milliSeconds
65 | * a duration specified by a number of milliseconds.
66 | *
67 | * @return a fuzzy string representing the duration between two events.
68 | */
69 | String formatDuration(long milliSeconds);
70 |
71 | /**
72 | * Creates a string representing a duration. Use this method to tell the
73 | * user how much time has passes between two events. This method uses the
74 | * strings resources to format the string.
75 | *
76 | * @param relative
77 | * the given related date/time.
78 | *
79 | * @return a fuzzy string representing the duration between the given and
80 | * the current date/time.
81 | */
82 | String formatDuration(Date relative);
83 |
84 | }
85 |
--------------------------------------------------------------------------------
/fuzzydateandroid-lib/src/com/fuzzydate/android/impl/.svn/all-wcprops:
--------------------------------------------------------------------------------
1 | K 25
2 | svn:wc:ra_dav:version-url
3 | V 74
4 | /svnroot/jfuzzydate/!svn/ver/10/trunk/src/main/java/net/sf/jfuzzydate/impl
5 | END
6 | DefaultFuzzingConfiguration.java
7 | K 25
8 | svn:wc:ra_dav:version-url
9 | V 106
10 | /svnroot/jfuzzydate/!svn/ver/5/trunk/src/main/java/net/sf/jfuzzydate/impl/DefaultFuzzingConfiguration.java
11 | END
12 | FuzzyDateFormatterImpl.java
13 | K 25
14 | svn:wc:ra_dav:version-url
15 | V 102
16 | /svnroot/jfuzzydate/!svn/ver/10/trunk/src/main/java/net/sf/jfuzzydate/impl/FuzzyDateFormatterImpl.java
17 | END
18 | Range.java
19 | K 25
20 | svn:wc:ra_dav:version-url
21 | V 85
22 | /svnroot/jfuzzydate/!svn/ver/10/trunk/src/main/java/net/sf/jfuzzydate/impl/Range.java
23 | END
24 |
--------------------------------------------------------------------------------
/fuzzydateandroid-lib/src/com/fuzzydate/android/impl/.svn/entries:
--------------------------------------------------------------------------------
1 | 10
2 |
3 | dir
4 | 40
5 | https://jfuzzydate.svn.sourceforge.net/svnroot/jfuzzydate/trunk/src/main/java/net/sf/jfuzzydate/impl
6 | https://jfuzzydate.svn.sourceforge.net/svnroot/jfuzzydate
7 |
8 |
9 |
10 | 2010-05-02T20:16:01.178418Z
11 | 10
12 | amaasch
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 | 304ab590-10bd-48d9-9485-c5c0298202dd
28 |
29 | DefaultFuzzingConfiguration.java
30 | file
31 |
32 |
33 |
34 |
35 | 2012-02-16T07:31:49.000000Z
36 | 694a847a2c6de3c69253e536fca480ff
37 | 2010-04-16T14:55:44.273276Z
38 | 5
39 | amaasch
40 | has-props
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 | 6458
62 |
63 | FuzzyDateFormatterImpl.java
64 | file
65 |
66 |
67 |
68 |
69 | 2012-02-16T07:31:49.000000Z
70 | 7f9d24994d2df8417517cecd99df43e2
71 | 2010-05-02T20:16:01.178418Z
72 | 10
73 | amaasch
74 | has-props
75 |
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 |
85 |
86 |
87 |
88 |
89 |
90 |
91 |
92 |
93 |
94 |
95 | 5936
96 |
97 | Range.java
98 | file
99 |
100 |
101 |
102 |
103 | 2012-02-16T07:31:49.000000Z
104 | 7ceed55bf9a296f2e61a8dfe8c888f9d
105 | 2010-05-02T20:16:01.178418Z
106 | 10
107 | amaasch
108 | has-props
109 |
110 |
111 |
112 |
113 |
114 |
115 |
116 |
117 |
118 |
119 |
120 |
121 |
122 |
123 |
124 |
125 |
126 |
127 |
128 |
129 | 1827
130 |
131 |
--------------------------------------------------------------------------------
/fuzzydateandroid-lib/src/com/fuzzydate/android/impl/.svn/prop-base/DefaultFuzzingConfiguration.java.svn-base:
--------------------------------------------------------------------------------
1 | K 13
2 | svn:mime-type
3 | V 10
4 | text/plain
5 | END
6 |
--------------------------------------------------------------------------------
/fuzzydateandroid-lib/src/com/fuzzydate/android/impl/.svn/prop-base/FuzzyDateFormatterImpl.java.svn-base:
--------------------------------------------------------------------------------
1 | K 13
2 | svn:mime-type
3 | V 10
4 | text/plain
5 | END
6 |
--------------------------------------------------------------------------------
/fuzzydateandroid-lib/src/com/fuzzydate/android/impl/.svn/prop-base/Range.java.svn-base:
--------------------------------------------------------------------------------
1 | K 13
2 | svn:mime-type
3 | V 10
4 | text/plain
5 | END
6 |
--------------------------------------------------------------------------------
/fuzzydateandroid-lib/src/com/fuzzydate/android/impl/.svn/text-base/DefaultFuzzingConfiguration.java.svn-base:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/scottyab/FuzzyDateAndroid/b7955bb56f2efc2b6e33888c20f1348a8213d2ee/fuzzydateandroid-lib/src/com/fuzzydate/android/impl/.svn/text-base/DefaultFuzzingConfiguration.java.svn-base
--------------------------------------------------------------------------------
/fuzzydateandroid-lib/src/com/fuzzydate/android/impl/.svn/text-base/FuzzyDateFormatterImpl.java.svn-base:
--------------------------------------------------------------------------------
1 | package net.sf.jfuzzydate.impl;
2 |
3 | import java.util.Date;
4 | import java.util.Locale;
5 |
6 | import net.sf.jfuzzydate.FuzzingConfiguration;
7 | import net.sf.jfuzzydate.FuzzingStrength;
8 | import net.sf.jfuzzydate.FuzzyDateFormatter;
9 |
10 | /**
11 | * Basic implementation for fuzzy date formatting.
12 | *
13 | * @author amaasch
14 | *
15 | * @see FuzzyDateFormatter
16 | */
17 | public class FuzzyDateFormatterImpl implements FuzzyDateFormatter {
18 |
19 | /**
20 | * The fuzzing configuration.
21 | */
22 | private final FuzzingConfiguration config;
23 |
24 | /**
25 | * Creates a new FuzzyDateFormatterImpl object with the specified
26 | * configuration.
27 | *
28 | * @param config
29 | * the given configuration.
30 | */
31 | public FuzzyDateFormatterImpl(final FuzzingConfiguration config) {
32 | this.config = config;
33 | }
34 |
35 | /*
36 | * (non-Javadoc)
37 | *
38 | * @see net.sf.jfuzzydate.FuzzyDateFormatter#format(java.util.Date)
39 | */
40 | public String format(final Date date) {
41 | return format(date, Locale.getDefault());
42 | }
43 |
44 | /*
45 | * (non-Javadoc)
46 | *
47 | * @see net.sf.jfuzzydate.FuzzyDateFormatter#format(java.util.Date,
48 | * java.util.Locale)
49 | */
50 | public String format(final Date date, final Locale locale) {
51 | return formatDate(date.getTime() - new Date().getTime(),
52 | FuzzingStrength.NORMAL, locale);
53 | }
54 |
55 | /**
56 | * Formats a point in time. Not yet implemented.
57 | *
58 | * @param distance
59 | * todo
60 | * @param strength
61 | * the strength of the fuzzyfication.
62 | * @param locale
63 | * the locale for formatting.
64 | *
65 | * @return a string representing a point in time in relation to the current
66 | * time.
67 | */
68 | private String formatDate(final long distance,
69 | final FuzzingStrength strength, final Locale locale) {
70 | return "not implemented";
71 | }
72 |
73 | /*
74 | * (non-Javadoc)
75 | *
76 | * @see net.sf.jfuzzydate.FuzzyDateFormatter#formatDistance(java.util.Date)
77 | */
78 | public String formatDistance(final Date date) {
79 | return formatDistance(date, Locale.getDefault());
80 | }
81 |
82 | /*
83 | * (non-Javadoc)
84 | *
85 | * @see net.sf.jfuzzydate.FuzzyDateFormatter#formatDistance(java.util.Date,
86 | * java.util.Locale)
87 | */
88 | public String formatDistance(final Date date, final Locale locale) {
89 | return formatDistance(date.getTime() - new Date().getTime(),
90 | FuzzingStrength.NORMAL, locale);
91 | }
92 |
93 | /**
94 | * Formats a distance.
95 | *
96 | * @param distanceSec
97 | * a relative distance (number of milliseconds). Negative
98 | * distances are assumed to be past values.
99 | * @param strength
100 | * the strength of the fuzzyfication.
101 | * @param locale
102 | * the locale for formatting.
103 | *
104 | * @return a string representing a readable distance.
105 | */
106 | private String formatDistance(final long distanceSec,
107 | final FuzzingStrength strength, final Locale locale) {
108 | String pattern;
109 |
110 | if (distanceSec <= 0) {
111 | pattern = "distance.past.pattern";
112 | } else {
113 | pattern = "distance.future.pattern";
114 | }
115 |
116 | final long absDistance = Math.abs(distanceSec);
117 |
118 | for (final Range range : config.getDistanceRanges(strength)) {
119 | if (absDistance < range.getUpperBound()) {
120 | if (range.hasDivisor()) {
121 | final int parameter = Math.round(absDistance
122 | / range.getParameterDivisor());
123 |
124 | return config.getFuzzyString(pattern, locale, config
125 | .getFuzzyString(range, locale, Integer
126 | .valueOf(parameter)));
127 | }
128 |
129 | return config.getFuzzyString(pattern, locale, config
130 | .getFuzzyString(range, locale));
131 | }
132 | }
133 |
134 | return config.getFuzzyString(pattern, locale, config.getFuzzyString(
135 | Range.ETERNAL, locale));
136 | }
137 |
138 | /*
139 | * (non-Javadoc)
140 | *
141 | * @see net.sf.jfuzzydate.FuzzyDateFormatter#formatDuration(java.util.Date)
142 | */
143 | public String formatDuration(final Date relative) {
144 | return formatDuration(relative, Locale.getDefault());
145 | }
146 |
147 | /*
148 | * (non-Javadoc)
149 | *
150 | * @see net.sf.jfuzzydate.FuzzyDateFormatter#formatDuration(java.util.Date,
151 | * java.util.Date, java.util.Locale)
152 | */
153 | public String formatDuration(final Date from, final Date to,
154 | final Locale locale) {
155 | return formatDuration(to.getTime() - from.getTime(), locale);
156 | }
157 |
158 | /*
159 | * (non-Javadoc)
160 | *
161 | * @see net.sf.jfuzzydate.FuzzyDateFormatter#formatDuration(java.util.Date,
162 | * java.util.Locale)
163 | */
164 | public String formatDuration(final Date relative, final Locale locale) {
165 | return formatDuration(new Date().getTime() - relative.getTime(), locale);
166 | }
167 |
168 | /*
169 | * (non-Javadoc)
170 | *
171 | * @see net.sf.jfuzzydate.FuzzyDateFormatter#formatDuration(long)
172 | */
173 | public String formatDuration(final long milliSeconds) {
174 | return formatDuration(milliSeconds, Locale.getDefault());
175 | }
176 |
177 | /**
178 | * Formats a duration.
179 | *
180 | * @param milliSeconds
181 | * the duration defined by a number of milliseconds. Negative
182 | * distances are assumed to be past values.
183 | * @param strength
184 | * the strength of the fuzzyfication.
185 | * @param locale
186 | * the locale for formatting.
187 | *
188 | * @return a string representing a readable duration.
189 | */
190 | private String formatDuration(final long milliSeconds,
191 | final FuzzingStrength strength, final Locale locale) {
192 | for (final Range range : config.getDurationRanges(strength)) {
193 | if (milliSeconds < range.getUpperBound()) {
194 | if (range.hasDivisor()) {
195 | final int parameter = Math.round(milliSeconds
196 | / range.getParameterDivisor());
197 |
198 | return config.getFuzzyString(range, locale, Integer
199 | .valueOf(parameter));
200 | }
201 |
202 | return config.getFuzzyString(range, locale);
203 | }
204 | }
205 |
206 | return config.getFuzzyString(Range.ETERNAL, locale);
207 | }
208 |
209 | /*
210 | * (non-Javadoc)
211 | *
212 | * @see net.sf.jfuzzydate.FuzzyDateFormatter#formatDuration(long,
213 | * java.util.Locale)
214 | */
215 | public String formatDuration(final long milliSeconds, final Locale locale) {
216 | return formatDuration(milliSeconds, FuzzingStrength.NORMAL, locale);
217 | }
218 | }
219 |
--------------------------------------------------------------------------------
/fuzzydateandroid-lib/src/com/fuzzydate/android/impl/.svn/text-base/Range.java.svn-base:
--------------------------------------------------------------------------------
1 | package net.sf.jfuzzydate.impl;
2 |
3 | /**
4 | * A date/time range mapping time values up to an upper bound to an externalized
5 | * string.
6 | *
7 | * @author amaasch
8 | */
9 | public class Range {
10 |
11 | /**
12 | *
13 | */
14 | public static final Range ETERNAL = new Range(Long.MAX_VALUE,
15 | "distance.eternal");
16 |
17 | /**
18 | *
19 | */
20 | private Long parameterDivisor;
21 |
22 | /**
23 | * The key for the externalized string.
24 | */
25 | private final String bundleKey;
26 |
27 | /**
28 | *
29 | */
30 | private final long upperBound;
31 |
32 | /**
33 | * Creates a new Range object with an upper binding and a reference to an
34 | * externalized string.
35 | *
36 | * @param upperBound
37 | * @param bundleKey
38 | */
39 | public Range(final long upperBound, final String bundleKey) {
40 | this.upperBound = upperBound * 1000;
41 | this.bundleKey = bundleKey;
42 | }
43 |
44 | /**
45 | * Creates a new Range object with an upper binding and a reference to an
46 | * externalized string. This constructor method also sets a divisor, used to
47 | * define the calculation of number values included in fuzzy strings.
48 | *
49 | * @param upperBound
50 | * @param bundleKey
51 | * @param parameterDivisor
52 | */
53 | public Range(final long upperBound, final String bundleKey,
54 | final long parameterDivisor) {
55 | this.upperBound = upperBound * 1000;
56 | this.bundleKey = bundleKey;
57 | this.parameterDivisor = parameterDivisor * 1000;
58 | }
59 |
60 | /**
61 | *
62 | *
63 | * @return the bundleKey
64 | */
65 | public String getBundleKey() {
66 | return bundleKey;
67 | }
68 |
69 | /**
70 | *
71 | *
72 | * @return the parameterDivisor
73 | */
74 | public long getParameterDivisor() {
75 | return parameterDivisor;
76 | }
77 |
78 | /**
79 | *
80 | *
81 | * @return the upperBound
82 | */
83 | public long getUpperBound() {
84 | return upperBound;
85 | }
86 |
87 | /**
88 | *
89 | *
90 | * @return
91 | */
92 | public boolean hasDivisor() {
93 | return parameterDivisor != null;
94 | }
95 | }
96 |
--------------------------------------------------------------------------------
/fuzzydateandroid-lib/src/com/fuzzydate/android/impl/DefaultFuzzingConfiguration.java:
--------------------------------------------------------------------------------
1 | package com.fuzzydate.android.impl;
2 |
3 | import android.content.Context;
4 |
5 | import com.fuzzydate.android.FuzzingConfiguration;
6 | import com.fuzzydate.android.FuzzingStrength;
7 | import com.fuzzydate.android.R;
8 |
9 | /**
10 | * A very simple and static configuration for fuzzy date formatting_
11 | *
12 | *
13 | *
14 | * @author ma�
15 | */
16 | public final class DefaultFuzzingConfiguration implements FuzzingConfiguration {
17 | // ~ Static fields/initializers
18 | // ---------------------------------------------
19 |
20 | /**
21 | * The singleton instance_
22 | */
23 | private static DefaultFuzzingConfiguration instance;
24 |
25 | // ~ Instance fields
26 | // --------------------------------------------------------
27 |
28 | /**
29 | *
30 | */
31 | private final Range[] dist_normal;
32 |
33 | /**
34 | *
35 | */
36 | private final Range[] dur_normal;
37 |
38 | private final Range[] hour_normal;
39 |
40 | // ~ Constructors
41 | // -----------------------------------------------------------
42 |
43 | /**
44 | * Creates a new DefaultFuzzingConfiguration object_
45 | */
46 | private DefaultFuzzingConfiguration() {
47 | final int min = 60;
48 | final int hour = 60 * min;
49 | final int day = 24 * hour;
50 | final int week = 7 * day;
51 | final int month = 30 * day;
52 |
53 | dur_normal = new Range[] {
54 | new Range(80, R.string.fuzzy__duration_minute_1),
55 | new Range(140, R.string.fuzzy__duration_minute_2),
56 | new Range(40 * min, R.string.fuzzy__duration_minute_x, min),
57 | new Range(90 * min, R.string.fuzzy__duration_hour_1),
58 | new Range(150 * min, R.string.fuzzy__duration_hour_2),
59 | new Range(day, R.string.fuzzy__duration_hour_x, hour),
60 | new Range(36 * hour, R.string.fuzzy__duration_day_1),
61 | new Range(60 * hour, R.string.fuzzy__duration_day_2),
62 | new Range(week, R.string.fuzzy__duration_day_x, day),
63 | new Range(11 * day, R.string.fuzzy__duration_week_1),
64 | new Range(18 * day, R.string.fuzzy__duration_week_2),
65 | new Range(4 * week, R.string.fuzzy__duration_week_x, week),
66 | new Range(45 * day, R.string.fuzzy__duration_month_1),
67 | new Range(75 * day, R.string.fuzzy__duration_month_2),
68 | new Range(300 * day, R.string.fuzzy__duration_month_x, month),
69 | new Range(Long.MAX_VALUE, R.string.fuzzy__duration_eternal) };
70 |
71 | dist_normal = new Range[] {
72 | new Range(80, R.string.fuzzy__distance_minute_1),
73 | new Range(140, R.string.fuzzy__distance_minute_2),
74 | new Range(40 * min, R.string.fuzzy__distance_minute_x, min),
75 | new Range(90 * min, R.string.fuzzy__distance_hour_1),
76 | new Range(150 * min, R.string.fuzzy__distance_hour_2),
77 | new Range(day, R.string.fuzzy__distance_hour_x, hour),
78 | new Range(36 * hour, R.string.fuzzy__distance_day_1),
79 | new Range(60 * hour, R.string.fuzzy__distance_day_2),
80 | new Range(week, R.string.fuzzy__distance_day_x, day),
81 | new Range(11 * day, R.string.fuzzy__distance_week_1),
82 | new Range(18 * day, R.string.fuzzy__distance_week_2),
83 | new Range(4 * week, R.string.fuzzy__distance_week_x, week),
84 | new Range(45 * day, R.string.fuzzy__distance_month_1),
85 | new Range(75 * day, R.string.fuzzy__distance_month_2),
86 | new Range(300 * day, R.string.fuzzy__distance_month_x, month),
87 | new Range(Long.MAX_VALUE, R.string.fuzzy__duration_eternal) };
88 |
89 | hour_normal = new Range[] { new Range(0, R.string.fuzzy__hour_0),
90 | new Range(1, R.string.fuzzy__hour_1),
91 | new Range(2, R.string.fuzzy__hour_2),
92 | new Range(3, R.string.fuzzy__hour_3),
93 | new Range(4, R.string.fuzzy__hour_4),
94 | new Range(5, R.string.fuzzy__hour_5),
95 | new Range(6, R.string.fuzzy__hour_6),
96 | new Range(7, R.string.fuzzy__hour_7),
97 | new Range(8, R.string.fuzzy__hour_8),
98 | new Range(9, R.string.fuzzy__hour_9),
99 | new Range(10, R.string.fuzzy__hour_10),
100 | new Range(11, R.string.fuzzy__hour_11),
101 | new Range(12, R.string.fuzzy__hour_12),
102 | new Range(13, R.string.fuzzy__hour_13),
103 | new Range(14, R.string.fuzzy__hour_14),
104 | new Range(15, R.string.fuzzy__hour_15),
105 | new Range(16, R.string.fuzzy__hour_16),
106 | new Range(17, R.string.fuzzy__hour_17),
107 | new Range(18, R.string.fuzzy__hour_18),
108 | new Range(19, R.string.fuzzy__hour_19),
109 | new Range(20, R.string.fuzzy__hour_20),
110 | new Range(21, R.string.fuzzy__hour_21),
111 | new Range(22, R.string.fuzzy__hour_22),
112 | new Range(23, R.string.fuzzy__hour_23) };
113 |
114 | }
115 |
116 | // ~ Methods
117 | // ----------------------------------------------------------------
118 |
119 | /**
120 | * This static method returns a shared instance of this default
121 | * configuration class_
122 | *
123 | * @return a DefaultFuzzingConfiguration istance_
124 | */
125 | public static DefaultFuzzingConfiguration getInstance() {
126 | if (instance == null) {
127 | instance = new DefaultFuzzingConfiguration();
128 | }
129 |
130 | return instance;
131 | }
132 |
133 | /*
134 | * (non-Javadoc)
135 | *
136 | * @see
137 | * net_sf_jfuzzydate_FuzzingConfiguration#getDistanceRanges(net_sf_jfuzzydate
138 | * _FuzzingStrength)
139 | */
140 | public Range[] getDistanceRanges(FuzzingStrength strenght) {
141 | final Range[] ranges;
142 |
143 | switch (strenght) {
144 | case NORMAL:
145 | ranges = dist_normal;
146 |
147 | break;
148 |
149 | case STRONG:
150 | // TODO
151 | ranges = dist_normal;
152 |
153 | break;
154 |
155 | case EXTREME:
156 | // TODO
157 | ranges = dist_normal;
158 |
159 | break;
160 |
161 | default:
162 | ranges = dist_normal;
163 | }
164 |
165 | return ranges;
166 | }
167 |
168 | public Range[] getHourRanges(FuzzingStrength strenght) {
169 | final Range[] ranges;
170 |
171 | switch (strenght) {
172 | case NORMAL:
173 | ranges = hour_normal;
174 |
175 | break;
176 |
177 | case STRONG:
178 | // TODO
179 | ranges = hour_normal;
180 |
181 | break;
182 |
183 | case EXTREME:
184 | // TODO
185 | ranges = hour_normal;
186 |
187 | break;
188 |
189 | default:
190 | ranges = hour_normal;
191 | }
192 |
193 | return ranges;
194 | }
195 |
196 | /*
197 | * (non-Javadoc)
198 | *
199 | * @see
200 | * net_sf_jfuzzydate_FuzzingConfiguration#getDurationRanges(net_sf_jfuzzydate
201 | * _FuzzingStrength)
202 | */
203 | public Range[] getDurationRanges(FuzzingStrength strenght) {
204 | final Range[] ranges;
205 |
206 | switch (strenght) {
207 | case NORMAL:
208 | ranges = dur_normal;
209 |
210 | break;
211 |
212 | case STRONG:
213 | // TODO
214 | ranges = dur_normal;
215 |
216 | break;
217 |
218 | case EXTREME:
219 | // TODO
220 | ranges = dur_normal;
221 |
222 | break;
223 |
224 | default:
225 | ranges = dur_normal;
226 | }
227 |
228 | return ranges;
229 | }
230 |
231 | /*
232 | * (non-Javadoc)
233 | *
234 | * @see
235 | * net_sf_jfuzzydate_FuzzingConfiguration#getFuzzyString(net_sf_jfuzzydate
236 | * _impl_Range, java_util_Locale, java_lang_Object[])
237 | */
238 | public String getFuzzyString(final Range range, final Context context,
239 | final Object... params) {
240 | return context.getResources().getString(range.getLabelResId(), params);
241 | }
242 |
243 | /*
244 | * (non-Javadoc)
245 | *
246 | * @see
247 | * net_sf_jfuzzydate_FuzzingConfiguration#getFuzzyString(java_lang_String,
248 | * java_util_Locale, java_lang_Object[])
249 | */
250 | public String getFuzzyString(final int resId, final Context context,
251 | final Object... params) {
252 |
253 | // int resId = context.getResources().getIdentifier(pattern, "string",
254 | // context.getPackageName());
255 |
256 | return context.getResources().getString(resId, params);
257 | }
258 | }
259 |
--------------------------------------------------------------------------------
/fuzzydateandroid-lib/src/com/fuzzydate/android/impl/FuzzyDateFormatterImpl.java:
--------------------------------------------------------------------------------
1 | package com.fuzzydate.android.impl;
2 |
3 | import java.util.Calendar;
4 | import java.util.Date;
5 |
6 | import android.content.Context;
7 |
8 | import com.fuzzydate.android.FuzzingConfiguration;
9 | import com.fuzzydate.android.FuzzingStrength;
10 | import com.fuzzydate.android.FuzzyDateFormatter;
11 | import com.fuzzydate.android.R;
12 |
13 | /**
14 | * Basic implementation for fuzzy date formatting.
15 | *
16 | * @author amaasch
17 | *
18 | * @see FuzzyDateFormatter
19 | */
20 | public class FuzzyDateFormatterImpl implements FuzzyDateFormatter {
21 |
22 | /**
23 | * The fuzzing configuration.
24 | */
25 | private final FuzzingConfiguration config;
26 | private Context context;
27 |
28 | /**
29 | * Creates a new FuzzyDateFormatterImpl object with the specified
30 | * configuration.
31 | *
32 | * @param config
33 | * the given configuration.
34 | */
35 | public FuzzyDateFormatterImpl(final FuzzingConfiguration config,
36 | Context context) {
37 | this.config = config;
38 | this.context = context;
39 | }
40 |
41 | /*
42 | * (non-Javadoc)
43 | *
44 | * @see net.sf.jfuzzydate.FuzzyDateFormatter#format(java.util.Date)
45 | */
46 | public String format(final Date date) {
47 | return formatDate(date.getTime(), FuzzingStrength.NORMAL);
48 | }
49 |
50 | /**
51 | * Formats a point in time.
52 | *
53 | * e.g 15:15 becomes quarter past three
54 | *
55 | * TODO: add exceptions for midday, midnight
56 | *
57 | * @param time
58 | * a time to make fuzzy
59 | *
60 | * @param strength
61 | * the strength of the fuzzyfication.
62 | *
63 | * @return a string representing a point in time
64 | */
65 | private String formatDate(final long time, final FuzzingStrength strength) {
66 |
67 | // e.g convert long of time to fuzzy six o'clock, quarter past six,
68 | // quarter to six
69 | Calendar cal = Calendar.getInstance();
70 | cal.setTimeInMillis(time);
71 |
72 | int hour = cal.get(Calendar.HOUR_OF_DAY);
73 | int mins = cal.get(Calendar.MINUTE);
74 |
75 | // which pattern is it?
76 | int pattern = 0;
77 | if (mins <= 14) {
78 | pattern = R.string.fuzzy__quarters_0_singular;
79 | } else if (mins >= 15 && mins < 29) {
80 | pattern = R.string.fuzzy__quarters_1;
81 | } else if (mins >= 30 && mins < 44) {
82 | pattern = R.string.fuzzy__quarters_2_singular;
83 | } else if (mins > 45) {
84 | pattern = R.string.fuzzy__quarters_3_singular;
85 | // its now quart to so roll the date, and handle quarter to 1
86 | cal.roll(Calendar.HOUR_OF_DAY, true);
87 | hour = cal.get(Calendar.HOUR_OF_DAY);
88 | }
89 |
90 | // to match the upperbound and in with the ranges of dist/duration
91 | final int absHour = Math.abs(hour) * 1000;
92 |
93 | // pick the correct range based on the hour.
94 | for (final Range range : config.getHourRanges(strength)) {
95 | if (absHour == range.getUpperBound()) {
96 | return config.getFuzzyString(pattern, context,
97 | config.getFuzzyString(range, context, absHour));
98 | }
99 | }
100 | return config.getFuzzyString(R.string.fuzzy__cannot_find_time, context);
101 | }
102 |
103 | /*
104 | * (non-Javadoc)
105 | *
106 | * @see net.sf.jfuzzydate.FuzzyDateFormatter#formatDistance(java.util.Date)
107 | */
108 | public String formatDistance(final Date date) {
109 | return formatDistance(date.getTime() - new Date().getTime(),
110 | FuzzingStrength.NORMAL);
111 | }
112 |
113 | /**
114 | * Formats a distance.
115 | *
116 | * @param distanceSec
117 | * a relative distance (number of milliseconds). Negative
118 | * distances are assumed to be past values.
119 | * @param strength
120 | * the strength of the fuzzyfication.
121 | * @param locale
122 | * the locale for formatting.
123 | *
124 | * @return a string representing a readable distance.
125 | */
126 | private String formatDistance(final long distanceSec,
127 | final FuzzingStrength strength) {
128 | int pattern;
129 |
130 | if (distanceSec <= 0) {
131 | pattern = R.string.fuzzy__distance_past_pattern;
132 | } else {
133 | pattern = R.string.fuzzy__distance_future_pattern;
134 | }
135 |
136 | final long absDistance = Math.abs(distanceSec);
137 |
138 | for (final Range range : config.getDistanceRanges(strength)) {
139 | if (absDistance < range.getUpperBound()) {
140 | if (range.hasDivisor()) {
141 | final int parameter = Math.round(absDistance
142 | / range.getParameterDivisor());
143 |
144 | return config.getFuzzyString(
145 | pattern,
146 | context,
147 | config.getFuzzyString(range, context,
148 | Integer.valueOf(parameter)));
149 | }
150 |
151 | return config.getFuzzyString(pattern, context,
152 | config.getFuzzyString(range, context));
153 | }
154 | }
155 |
156 | return config.getFuzzyString(pattern, context,
157 | config.getFuzzyString(Range.ETERNAL, context));
158 | }
159 |
160 | /*
161 | * (non-Javadoc)
162 | *
163 | * @see net.sf.jfuzzydate.FuzzyDateFormatter#formatDuration(java.util.Date)
164 | */
165 | public String formatDuration(final Date relative) {
166 | return formatDuration(relative.getTime(), FuzzingStrength.NORMAL);
167 | }
168 |
169 | /*
170 | * (non-Javadoc)
171 | *
172 | * @see net.sf.jfuzzydate.FuzzyDateFormatter#formatDuration(long)
173 | */
174 | public String formatDuration(final long milliSeconds) {
175 | return formatDuration(milliSeconds, FuzzingStrength.NORMAL);
176 | }
177 |
178 | /**
179 | * Formats a duration.
180 | *
181 | * @param milliSeconds
182 | * the duration defined by a number of milliseconds. Negative
183 | * distances are assumed to be past values.
184 | * @param strength
185 | * the strength of the fuzzyfication.
186 | *
187 | * @return a string representing a readable duration.
188 | */
189 | private String formatDuration(final long milliSeconds,
190 | final FuzzingStrength strength) {
191 | for (final Range range : config.getDurationRanges(strength)) {
192 | if (milliSeconds < range.getUpperBound()) {
193 | if (range.hasDivisor()) {
194 | final int parameter = Math.round(milliSeconds
195 | / range.getParameterDivisor());
196 |
197 | return config.getFuzzyString(range, context,
198 | Integer.valueOf(parameter));
199 | }
200 |
201 | return config.getFuzzyString(range, context);
202 | }
203 | }
204 | return config.getFuzzyString(Range.ETERNAL, context);
205 | }
206 |
207 | }
208 |
--------------------------------------------------------------------------------
/fuzzydateandroid-lib/src/com/fuzzydate/android/impl/Range.java:
--------------------------------------------------------------------------------
1 | package com.fuzzydate.android.impl;
2 |
3 | import com.fuzzydate.android.R;
4 |
5 | /**
6 | * A date/time range mapping time values up to an upper bound to an externalized
7 | * string.
8 | *
9 | * @author amaasch (updated by scottyab)
10 | *
11 | */
12 | public class Range {
13 |
14 | /**
15 | *
16 | */
17 | public static final Range ETERNAL = new Range(Long.MAX_VALUE,
18 | R.string.fuzzy__distance_eternal);
19 |
20 | /**
21 | *
22 | */
23 | private Long parameterDivisor;
24 |
25 | /**
26 | * Res id of the string label
27 | */
28 | private final int labelResId;
29 |
30 | /**
31 | *
32 | */
33 | private final long upperBound;
34 |
35 | /**
36 | * Creates a new Range object with an upper binding and a reference to an
37 | * externalized string.
38 | *
39 | * @param upperBound
40 | * @param bundleKey
41 | */
42 | public Range(final long upperBound, final int bundleKey) {
43 | this.upperBound = upperBound * 1000;
44 | this.labelResId = bundleKey;
45 | }
46 |
47 | /**
48 | * Creates a new Range object with an upper binding and a reference to an
49 | * externalized string. This constructor method also sets a divisor, used to
50 | * define the calculation of number values included in fuzzy strings.
51 | *
52 | * @param upperBound
53 | * @param bundleKey
54 | * @param parameterDivisor
55 | */
56 | public Range(final long upperBound, final int bundleKey,
57 | final long parameterDivisor) {
58 | this.upperBound = upperBound * 1000;
59 | this.labelResId = bundleKey;
60 | this.parameterDivisor = parameterDivisor * 1000;
61 | }
62 |
63 | /**
64 | *
65 | *
66 | * @return the bundleKey
67 | */
68 | public int getLabelResId() {
69 | return labelResId;
70 | }
71 |
72 | /**
73 | *
74 | *
75 | * @return the parameterDivisor
76 | */
77 | public long getParameterDivisor() {
78 | return parameterDivisor;
79 | }
80 |
81 | /**
82 | *
83 | *
84 | * @return the upperBound
85 | */
86 | public long getUpperBound() {
87 | return upperBound;
88 | }
89 |
90 | /**
91 | *
92 | *
93 | * @return
94 | */
95 | public boolean hasDivisor() {
96 | return parameterDivisor != null;
97 | }
98 | }
99 |
--------------------------------------------------------------------------------
/fuzzydateandroid-sample/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |