OpenConcerto

Dépôt officiel du code source de l'ERP OpenConcerto
sonarqube

svn://code.openconcerto.org/openconcerto

Rev

Rev 73 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 73 Rev 156
Line 16... Line 16...
16
import org.openconcerto.utils.Log;
16
import org.openconcerto.utils.Log;
17
 
17
 
18
import java.util.HashMap;
18
import java.util.HashMap;
19
import java.util.HashSet;
19
import java.util.HashSet;
20
import java.util.Map;
20
import java.util.Map;
-
 
21
import java.util.Objects;
21
import java.util.Set;
22
import java.util.Set;
22
 
23
 
23
import net.jcip.annotations.GuardedBy;
-
 
24
import net.jcip.annotations.ThreadSafe;
-
 
25
 
-
 
26
import com.ibm.icu.text.MessageFormat;
24
import com.ibm.icu.text.MessageFormat;
27
import com.ibm.icu.text.MessagePattern;
25
import com.ibm.icu.text.MessagePattern;
28
 
26
 
-
 
27
import net.jcip.annotations.GuardedBy;
-
 
28
import net.jcip.annotations.ThreadSafe;
-
 
29
 
29
/**
30
/**
30
 * A phrase and its declension. E.g. "light bulb or electrical outlet" and
31
 * A phrase and its declension. E.g. "light bulb or electrical outlet" and "light bulbs or
31
 * "light bulbs or electrical outlets".
32
 * electrical outlets".
32
 * 
33
 * 
33
 * @author Sylvain
34
 * @author Sylvain
34
 * @see <a href="Wikipedia">http://en.wikipedia.org/wiki/Declension</a>
35
 * @see <a href="Wikipedia">http://en.wikipedia.org/wiki/Declension</a>
35
 */
36
 */
36
@ThreadSafe
37
@ThreadSafe
Line 44... Line 45...
44
    private final String base;
45
    private final String base;
45
    private final NounClass nounClass;
46
    private final NounClass nounClass;
46
    @GuardedBy("this")
47
    @GuardedBy("this")
47
    private final Map<Object, String> variants;
48
    private final Map<Object, String> variants;
48
    @GuardedBy("this")
49
    @GuardedBy("this")
49
    private final Set<Object> explicitVariants;
50
    private final Set<VariantKey> explicitVariants;
50
 
51
 
51
    public Phrase(Grammar grammar, String base, NounClass nounClass) {
52
    public Phrase(Grammar grammar, String base, NounClass nounClass) {
52
        super();
53
        super();
53
        if (base == null)
54
        if (base == null)
54
            throw new NullPointerException("null base");
55
            throw new NullPointerException("null base");
Line 59... Line 60...
59
            this.variants = null;
60
            this.variants = null;
60
            this.explicitVariants = null;
61
            this.explicitVariants = null;
61
        } else {
62
        } else {
62
            this.variants = new HashMap<Object, String>();
63
            this.variants = new HashMap<Object, String>();
63
            this.variants.put(null, this.getBase());
64
            this.variants.put(null, this.getBase());
64
            this.explicitVariants = new HashSet<Object>();
65
            this.explicitVariants = new HashSet<>();
65
        }
66
        }
66
    }
67
    }
67
 
68
 
68
    public final Grammar getGrammar() {
69
    public final Grammar getGrammar() {
69
        return this.grammar;
70
        return this.grammar;
Line 86... Line 87...
86
     */
87
     */
87
    public final String putVariant(final VariantKey key, final String variant) {
88
    public final String putVariant(final VariantKey key, final String variant) {
88
        return this.putVariant(key, variant, true);
89
        return this.putVariant(key, variant, true);
89
    }
90
    }
90
 
91
 
-
 
92
    /**
-
 
93
     * Put a variant only if needed, i.e. if {@link #getVariant(VariantKey)} doesn't already return
-
 
94
     * <code>variant</code>. This is useful to keep {@link #getExplicitVariants()} to a minimum.
-
 
95
     * 
-
 
96
     * @param key which variant, e.g. plural.
-
 
97
     * @param variant the value, e.g. feet.
-
 
98
     * @return <code>true</code> if the variant was put.
-
 
99
     */
-
 
100
    public final synchronized boolean putVariantIfDifferent(final VariantKey key, final String variant) {
-
 
101
        final boolean diff = !variant.equals(this.getVariant(key));
-
 
102
        if (diff) {
-
 
103
            this.putVariant(key, variant);
-
 
104
        }
-
 
105
        return diff;
-
 
106
    }
-
 
107
 
91
    private final synchronized String putVariant(final VariantKey key, final String variant, final boolean explicit) {
108
    private final synchronized String putVariant(final VariantKey key, final String variant, final boolean explicit) {
92
        final String res = this.variants.put(key, variant);
109
        final String res = this.variants.put(key, variant);
93
        if (explicit) {
110
        if (explicit) {
94
            this.explicitVariants.add(key);
111
            this.explicitVariants.add(key);
95
            // remove computed variants
112
            // remove computed variants
Line 97... Line 114...
97
        }
114
        }
98
        return res;
115
        return res;
99
    }
116
    }
100
 
117
 
101
    /**
118
    /**
-
 
119
     * The variants that have been explicitly set by {@link #putVariant(VariantKey, String)}, as
-
 
120
     * opposed to variants computed automatically by the {@link #getGrammar() grammar}.
-
 
121
     * 
-
 
122
     * @return all explicit variants.
-
 
123
     */
-
 
124
    public synchronized Set<VariantKey> getExplicitVariants() {
-
 
125
        return this.explicitVariants == null ? null : new HashSet<>(this.explicitVariants);
-
 
126
    }
-
 
127
 
-
 
128
    /**
102
     * Get a variant. If the asked variant wasn't put by {@link #putVariant(VariantKey, String)},
129
     * Get a variant. If the asked variant wasn't put by {@link #putVariant(VariantKey, String)},
103
     * the {@link #getGrammar() grammar} is {@link Grammar#getVariant(Phrase, VariantKey) used}.
130
     * the {@link #getGrammar() grammar} is {@link Grammar#getVariant(Phrase, VariantKey) used}.
104
     * 
131
     * 
105
     * @param key which variant.
132
     * @param key which variant.
106
     * @return the asked variant.
133
     * @return the asked variant.
Line 138... Line 165...
138
            return new MessageFormat(getVariant(key), getGrammar().getLocale()).format(new Object[] { count });
165
            return new MessageFormat(getVariant(key), getGrammar().getLocale()).format(new Object[] { count });
139
        }
166
        }
140
    }
167
    }
141
 
168
 
142
    @Override
169
    @Override
-
 
170
    public synchronized int hashCode() {
-
 
171
        return Objects.hash(this.base, this.explicitVariants, this.grammar, this.nounClass);
-
 
172
    }
-
 
173
 
-
 
174
    @Override
-
 
175
    public synchronized boolean equals(Object obj) {
-
 
176
        if (this == obj)
-
 
177
            return true;
-
 
178
        if (obj == null)
-
 
179
            return false;
-
 
180
        if (getClass() != obj.getClass())
-
 
181
            return false;
-
 
182
        final Phrase o = (Phrase) obj;
-
 
183
        final boolean fast = Objects.equals(this.base, o.base) && Objects.equals(this.explicitVariants, o.explicitVariants) && Objects.equals(this.grammar, o.grammar)
-
 
184
                && Objects.equals(this.nounClass, o.nounClass);
-
 
185
        if (!fast || this.variants == null)
-
 
186
            return fast;
-
 
187
        for (final VariantKey e : this.explicitVariants) {
-
 
188
            if (!Objects.equals(this.variants.get(e), o.variants.get(e)))
-
 
189
                return false;
-
 
190
        }
-
 
191
        return true;
-
 
192
    }
-
 
193
 
-
 
194
    @Override
143
    public String toString() {
195
    public String toString() {
144
        final String cl = this.getNounClass() == null ? " " : " (" + this.getNounClass().getName() + ") ";
196
        final String cl = this.getNounClass() == null ? " " : " (" + this.getNounClass().getName() + ") ";
145
        final String gr = this.getGrammar() == null ? "" : " with " + this.getGrammar();
197
        final String gr = this.getGrammar() == null ? "" : " with " + this.getGrammar();
146
        return this.getClass().getSimpleName() + cl + this.getBase() + gr;
198
        return this.getClass().getSimpleName() + cl + this.getBase() + gr;
147
    }
199
    }