public class NonsenseBuilder extends java.lang.Object implements NonsenseGenerator
NonsenseGenerator
.
The builder will assemble a sentence of the following form:
adjective subject adverb verb adjective
object phrase. The terms listed in bold (subject, verb, object)
will always be rendered, while the terms in italics (adjectives, adverbs,
additional phrases) will be added randomly based on the builder's
configuration, which can be tuned by creating a custom instance of the
NonsenseBuilder.Configuration
inner class.
The class uses the following string resources to get its random words:
R.array#asNonsenseNouns
R.array#asNonsenseVerbs
R.array#asNonsenseAdjectives
R.array#asNonsenseAdverbs
R.array#asNonsensePhrases
Applications using this class may choose to have overrides for any or all of these string resources, in order to customize the text that might be rendered in the randomized sentences.
Modifier and Type | Class and Description |
---|---|
static class |
NonsenseBuilder.Configuration
This class controls aspects of a
NonsenseBuilder related to the
probability of certain random features. |
NonsenseGenerator.Utils
Modifier and Type | Field and Description |
---|---|
protected static NonsenseBuilder.Configuration |
CANONICAL_CONFIGURATION
A canonical instance of a
NonsenseBuilder.Configuration , using
the default values defined in that inner class. |
protected NonsenseBuilder.Configuration |
m_cfg
The configuration settings that will control the random elements of the
builder.
|
protected android.content.Context |
m_ctx
A context in which string resources are available.
|
protected java.lang.String |
m_sObject
(noun) The object of the sentence.
|
protected java.lang.String |
m_sObjectAdjective
(adjective) A modifier of the object of the sentence.
|
protected java.lang.String |
m_sObjectModifier
(prepositional phrase) A phrase further modifying the object of the
sentence.
|
protected java.lang.String |
m_sSubject
(noun) The subject of the sentence.
|
protected java.lang.String |
m_sSubjectAdjective
(adjective) A modifier of the subject.
|
protected java.lang.String |
m_sVerb
(verb) The action verb in the sentence.
|
protected java.lang.String |
m_sVerbAdverb
(adverb) A modifier of the sentence's verb.
|
protected static java.util.Random |
RANDOM
The internal RNG of the builder, used to determine the value selected for
each item in the sentence, and the presence of certain optional items.
|
Constructor and Description |
---|
NonsenseBuilder(android.content.Context ctx)
A constructor which sets the resource context.
|
NonsenseBuilder(android.content.Context ctx,
NonsenseBuilder.Configuration cfg)
A constructor which sets both the resource context and a custom
configuration to control the random aspects of the builder.
|
Modifier and Type | Method and Description |
---|---|
protected NonsenseBuilder |
appendObject(java.lang.StringBuilder sb)
Appends the object and its adjective and modifier phrase (if any) to the
buffer in which the sentence is being constructed.
|
protected NonsenseBuilder |
appendSubject(java.lang.StringBuilder sb)
Appends the subject and its adjective (if any) to the buffer in which the
sentence is being constructed.
|
protected NonsenseBuilder |
appendVerb(java.lang.StringBuilder sb)
Appends the verb and its adverb (if any) to the buffer in which the
sentence is being constructed.
|
java.lang.String |
getRandomNonsense(int resStrings,
int nChance)
Randomly selects a string resource from the selected string array.
|
java.lang.String |
getString()
Generates the nonsense string.
|
NonsenseBuilder |
setAdverb(java.lang.String sAdverb)
Locks a value for the adverb modifying the verb of the sentence.
|
NonsenseBuilder |
setConfiguration(NonsenseBuilder.Configuration cfg)
Sets the configuration parameters of the builder.
|
NonsenseBuilder |
setContext(android.content.Context ctx)
Sets the context in which string resources can be fetched.
|
NonsenseBuilder |
setObject(java.lang.String sObject)
Locks a value for the object of the sentence.
|
NonsenseBuilder |
setObjectAdjective(java.lang.String sAdj)
Locks a value for the adjective modifying the object of the sentence.
|
NonsenseBuilder |
setObjectModifier(java.lang.String sPhrase)
Locks a value for additional text (expected to be a prepositional phrase)
modifying the object of the sentence.
|
NonsenseBuilder |
setSubject(java.lang.String sSubject)
Locks a value for the subject of the sentence.
|
NonsenseBuilder |
setSubjectAdjective(java.lang.String sAdj)
Locks a value for the adjective modifying the subject of the sentence.
|
NonsenseBuilder |
setVerb(java.lang.String sVerb)
Locks a value for the verb of the sentence.
|
protected static final NonsenseBuilder.Configuration CANONICAL_CONFIGURATION
NonsenseBuilder.Configuration
, using
the default values defined in that inner class.protected static final java.util.Random RANDOM
protected android.content.Context m_ctx
protected NonsenseBuilder.Configuration m_cfg
CANONICAL_CONFIGURATION
.protected java.lang.String m_sSubject
protected java.lang.String m_sSubjectAdjective
protected java.lang.String m_sVerb
protected java.lang.String m_sVerbAdverb
protected java.lang.String m_sObject
protected java.lang.String m_sObjectAdjective
protected java.lang.String m_sObjectModifier
public NonsenseBuilder(android.content.Context ctx)
ctx
- a context in which string resources are availablepublic NonsenseBuilder(android.content.Context ctx, NonsenseBuilder.Configuration cfg)
ctx
- a context in which string resources are availablecfg
- the configuration parameters of the builderpublic NonsenseBuilder setConfiguration(NonsenseBuilder.Configuration cfg)
cfg
- specifies the randomization parameters for the builderpublic NonsenseBuilder setContext(android.content.Context ctx)
NonsenseGenerator
setContext
in interface NonsenseGenerator
ctx
- the context in which string resources are availablepublic NonsenseBuilder setSubject(java.lang.String sSubject)
null
is specified, then the builder's subject will be
"cleared", meaning that it will be chosen at random.sSubject
- a specific subject for the sentence, or null
to
ensure that the subject is randomizedpublic NonsenseBuilder setSubjectAdjective(java.lang.String sAdj)
null
is specified, then the builder might or might not add a
random adjective, as specified by
NonsenseBuilder.Configuration.m_nSubjectAdjectiveChance
.sAdj
- a specific adjective to modify the subject of the sentence,
or null
to randomize the presence and value of the
adjectivepublic NonsenseBuilder setVerb(java.lang.String sVerb)
null
is specified, then the builder's verb will be "cleared",
meaning that it will be chosen at random.sVerb
- a specific verb for the sentence, or null
to ensure
that the verb is randomizedpublic NonsenseBuilder setAdverb(java.lang.String sAdverb)
null
is specified, then the builder might or might not add a
random adverb, as specified by NonsenseBuilder.Configuration.m_nAdverbChance
.sAdverb
- a specific adverb to modify the verb of the sentence, or
null
to randomize the presence and value of the
adverbpublic NonsenseBuilder setObject(java.lang.String sObject)
null
is specified, then the builder's object will be
"cleared", meaning that it will be chosen at random.sObject
- a specific object for the sentence, or null
to
ensure that the object is randomizedpublic NonsenseBuilder setObjectAdjective(java.lang.String sAdj)
null
is specified, then the builder might or might not add a
random adjective, as specified by
NonsenseBuilder.Configuration.m_nObjectAdjectiveChance
.sAdj
- a specific adjective to modify the object of the sentence, or
null
to randomize the presence and value of the
adjectivepublic NonsenseBuilder setObjectModifier(java.lang.String sPhrase)
null
is specified, then the builder might or might not add a
random phrase, as specified by
NonsenseBuilder.Configuration.m_nObjectPhraseChance
.sPhrase
- specific text to modify the object of the sentence, or
null
to randomize the presence and value of such
textpublic java.lang.String getString()
NonsenseGenerator
getString
in interface NonsenseGenerator
protected NonsenseBuilder appendSubject(java.lang.StringBuilder sb)
sb
- the buffer in which the sentence is being constructedprotected NonsenseBuilder appendVerb(java.lang.StringBuilder sb)
sb
- the buffer in which the sentence is being constructedprotected NonsenseBuilder appendObject(java.lang.StringBuilder sb)
sb
- the buffer in which the sentence is being constructedpublic java.lang.String getRandomNonsense(int resStrings, int nChance)
resStrings
- the resource ID of a string arraynChance
- chance that we should return anything at all, expressed as
a percentage (expected range [0,100])