Skip to content

Commit 1c7dd96

Browse files
committed
Move some bare strings into constants
1 parent ac6ae9e commit 1c7dd96

File tree

1 file changed

+36
-28
lines changed

1 file changed

+36
-28
lines changed

ext/java/org/jruby/ext/psych/PsychParser.java

Lines changed: 36 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -99,26 +99,34 @@
9999
public class PsychParser extends RubyObject {
100100

101101
public static final String JRUBY_CALL_SITES = "_jruby_call_sites";
102+
public static final String ENCODING_ANY = "ANY";
103+
public static final String ENCODING_UTF8 = "UTF8";
104+
public static final String ENCODING_UTF16LE = "UTF16LE";
105+
public static final String ENCODING_UTF16BE = "UTF16BE";
106+
public static final String MAX_ALIASES_FOR_COLLECTIONS = "max_aliases_for_collections";
107+
public static final String ALLOW_DUPLICATE_KEYS = "allow_duplicate_keys";
108+
public static final String ALLOW_RECURSIVE_KEYS = "allow_recursive_keys";
109+
public static final String CODE_POINT_LIMIT = "code_point_limit";
102110

103111
public static void initPsychParser(Ruby runtime, RubyModule psych) {
104112
RubyClass psychParser = runtime.defineClassUnder("Parser", runtime.getObject(), PsychParser::new, psych);
105113

106114
psychParser.setInternalVariable(JRUBY_CALL_SITES, new CallSites());
107115

108116
runtime.getLoadService().require("psych/syntax_error");
109-
psychParser.defineConstant("ANY", runtime.newFixnum(YAML_ANY_ENCODING.ordinal()));
110-
psychParser.defineConstant("UTF8", runtime.newFixnum(YAML_UTF8_ENCODING.ordinal()));
111-
psychParser.defineConstant("UTF16LE", runtime.newFixnum(YAML_UTF16LE_ENCODING.ordinal()));
112-
psychParser.defineConstant("UTF16BE", runtime.newFixnum(YAML_UTF16BE_ENCODING.ordinal()));
117+
psychParser.defineConstant(ENCODING_ANY, runtime.newFixnum(YAML_ANY_ENCODING.ordinal()));
118+
psychParser.defineConstant(ENCODING_UTF8, runtime.newFixnum(YAML_UTF8_ENCODING.ordinal()));
119+
psychParser.defineConstant(ENCODING_UTF16LE, runtime.newFixnum(YAML_UTF16LE_ENCODING.ordinal()));
120+
psychParser.defineConstant(ENCODING_UTF16BE, runtime.newFixnum(YAML_UTF16BE_ENCODING.ordinal()));
113121

114122
psychParser.defineAnnotatedMethods(PsychParser.class);
115123

116124
// defaults for SnakeYAML load settings
117125
LoadSettings defaults = LoadSettings.builder().build();
118-
psychParser.setInternalVariable("max_aliases_for_collections", runtime.newFixnum(defaults.getMaxAliasesForCollections()));
119-
psychParser.setInternalVariable("allow_duplicate_keys", runtime.newBoolean(defaults.getAllowDuplicateKeys()));
120-
psychParser.setInternalVariable("allow_recursive_keys", runtime.newBoolean(defaults.getAllowRecursiveKeys()));
121-
psychParser.setInternalVariable("code_point_limit", runtime.newFixnum(defaults.getCodePointLimit()));
126+
psychParser.setInternalVariable(MAX_ALIASES_FOR_COLLECTIONS, runtime.newFixnum(defaults.getMaxAliasesForCollections()));
127+
psychParser.setInternalVariable(ALLOW_DUPLICATE_KEYS, runtime.newBoolean(defaults.getAllowDuplicateKeys()));
128+
psychParser.setInternalVariable(ALLOW_RECURSIVE_KEYS, runtime.newBoolean(defaults.getAllowRecursiveKeys()));
129+
psychParser.setInternalVariable(CODE_POINT_LIMIT, runtime.newFixnum(defaults.getCodePointLimit()));
122130
}
123131

124132
public PsychParser(Ruby runtime, RubyClass klass) {
@@ -129,10 +137,10 @@ public PsychParser(Ruby runtime, RubyClass klass) {
129137
// prepare settings builder and apply global defaults
130138
LoadSettingsBuilder lsb = LoadSettings.builder();
131139
lsb.setSchema(new CoreSchema());
132-
lsb.setMaxAliasesForCollections(((IRubyObject) klass.getInternalVariable("max_aliases_for_collections")).convertToInteger().getIntValue());
133-
lsb.setAllowDuplicateKeys(((IRubyObject) klass.getInternalVariable("allow_duplicate_keys")).isTrue());
134-
lsb.setAllowRecursiveKeys(((IRubyObject) klass.getInternalVariable("allow_recursive_keys")).isTrue());
135-
lsb.setCodePointLimit(((IRubyObject) klass.getInternalVariable("code_point_limit")).convertToInteger().getIntValue());
140+
lsb.setMaxAliasesForCollections(((IRubyObject) klass.getInternalVariable(MAX_ALIASES_FOR_COLLECTIONS)).convertToInteger().getIntValue());
141+
lsb.setAllowDuplicateKeys(((IRubyObject) klass.getInternalVariable(ALLOW_DUPLICATE_KEYS)).isTrue());
142+
lsb.setAllowRecursiveKeys(((IRubyObject) klass.getInternalVariable(ALLOW_RECURSIVE_KEYS)).isTrue());
143+
lsb.setCodePointLimit(((IRubyObject) klass.getInternalVariable(CODE_POINT_LIMIT)).convertToInteger().getIntValue());
136144
this.loadSettingsBuilder = lsb;
137145
}
138146

@@ -530,7 +538,7 @@ public IRubyObject max_aliases_for_collections_set(IRubyObject max) {
530538
return max;
531539
}
532540

533-
@JRubyMethod(name = "max_aliases_for_collections")
541+
@JRubyMethod(name = MAX_ALIASES_FOR_COLLECTIONS)
534542
public IRubyObject max_aliases_for_collections(ThreadContext context) {
535543
return context.runtime.newFixnum(buildSettings().getMaxAliasesForCollections());
536544
}
@@ -542,7 +550,7 @@ public IRubyObject allow_duplicate_keys_set(IRubyObject allow) {
542550
return allow;
543551
}
544552

545-
@JRubyMethod(name = "allow_duplicate_keys")
553+
@JRubyMethod(name = ALLOW_DUPLICATE_KEYS)
546554
public IRubyObject allow_duplicate_keys(ThreadContext context) {
547555
return RubyBoolean.newBoolean(context, buildSettings().getAllowDuplicateKeys());
548556
}
@@ -554,7 +562,7 @@ public IRubyObject allow_recursive_keys_set(IRubyObject allow) {
554562
return allow;
555563
}
556564

557-
@JRubyMethod(name = "allow_recursive_keys")
565+
@JRubyMethod(name = ALLOW_RECURSIVE_KEYS)
558566
public IRubyObject allow_recursive_keys(ThreadContext context) {
559567
return RubyBoolean.newBoolean(context, buildSettings().getAllowRecursiveKeys());
560568
}
@@ -566,7 +574,7 @@ public IRubyObject code_point_limit_set(IRubyObject limit) {
566574
return limit;
567575
}
568576

569-
@JRubyMethod(name = "code_point_limit")
577+
@JRubyMethod(name = CODE_POINT_LIMIT)
570578
public IRubyObject code_point_limit(ThreadContext context) {
571579
return context.runtime.newFixnum(buildSettings().getCodePointLimit());
572580
}
@@ -581,38 +589,38 @@ public static IRubyObject max_aliases_for_collections_set(ThreadContext context,
581589
throw context.runtime.newRangeError("max_aliases_for_collections must be positive");
582590
}
583591

584-
self.getInternalVariables().setInternalVariable("max_aliases_for_collections", max);
592+
self.getInternalVariables().setInternalVariable(MAX_ALIASES_FOR_COLLECTIONS, max);
585593

586594
return max;
587595
}
588596

589-
@JRubyMethod(name = "max_aliases_for_collections")
597+
@JRubyMethod(name = MAX_ALIASES_FOR_COLLECTIONS)
590598
public static IRubyObject max_aliases_for_collections(ThreadContext context, IRubyObject self) {
591-
return (IRubyObject) self.getInternalVariables().getInternalVariable("max_aliases_for_collections");
599+
return (IRubyObject) self.getInternalVariables().getInternalVariable(MAX_ALIASES_FOR_COLLECTIONS);
592600
}
593601

594602
@JRubyMethod(name = "allow_duplicate_keys=", meta = true)
595603
public static IRubyObject allow_duplicate_keys_set(IRubyObject self, IRubyObject allow) {
596-
self.getInternalVariables().setInternalVariable("allow_duplicate_keys", allow);
604+
self.getInternalVariables().setInternalVariable(ALLOW_DUPLICATE_KEYS, allow);
597605

598606
return allow;
599607
}
600608

601-
@JRubyMethod(name = "allow_duplicate_keys", meta = true)
609+
@JRubyMethod(name = ALLOW_DUPLICATE_KEYS, meta = true)
602610
public static IRubyObject allow_duplicate_keys(ThreadContext context, IRubyObject self) {
603-
return (IRubyObject) self.getInternalVariables().getInternalVariable("allow_duplicate_keys");
611+
return (IRubyObject) self.getInternalVariables().getInternalVariable(ALLOW_DUPLICATE_KEYS);
604612
}
605613

606614
@JRubyMethod(name = "allow_recursive_keys=", meta = true)
607615
public static IRubyObject allow_recursive_keys_set(IRubyObject self, IRubyObject allow) {
608-
self.getInternalVariables().setInternalVariable("allow_recursive_keys", allow);
616+
self.getInternalVariables().setInternalVariable(ALLOW_RECURSIVE_KEYS, allow);
609617

610618
return allow;
611619
}
612620

613-
@JRubyMethod(name = "allow_recursive_keys", meta = true)
621+
@JRubyMethod(name = ALLOW_RECURSIVE_KEYS, meta = true)
614622
public static IRubyObject allow_recursive_keys(ThreadContext context, IRubyObject self) {
615-
return (IRubyObject) self.getInternalVariables().getInternalVariable("allow_recursive_keys");
623+
return (IRubyObject) self.getInternalVariables().getInternalVariable(ALLOW_RECURSIVE_KEYS);
616624
}
617625

618626
@JRubyMethod(name = "code_point_limit=", meta = true)
@@ -623,14 +631,14 @@ public static IRubyObject code_point_limit_set(ThreadContext context, IRubyObjec
623631
throw context.runtime.newRangeError("code_point_limit must be positive");
624632
}
625633

626-
self.getInternalVariables().setInternalVariable("code_point_limit", limit);
634+
self.getInternalVariables().setInternalVariable(CODE_POINT_LIMIT, limit);
627635

628636
return limit;
629637
}
630638

631-
@JRubyMethod(name = "code_point_limit", meta = true)
639+
@JRubyMethod(name = CODE_POINT_LIMIT, meta = true)
632640
public static IRubyObject code_point_limit(ThreadContext context, IRubyObject self) {
633-
return (IRubyObject) self.getInternalVariables().getInternalVariable("code_point_limit");
641+
return (IRubyObject) self.getInternalVariables().getInternalVariable(CODE_POINT_LIMIT);
634642
}
635643

636644
private LoadSettings buildSettings() {

0 commit comments

Comments
 (0)