one-fits-all | The one-fits-all flavor for create-cycle-app
kandi X-RAY | one-fits-all Summary
kandi X-RAY | one-fits-all Summary
The one-fits-all flavor for create-cycle-app
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of one-fits-all
one-fits-all Key Features
one-fits-all Examples and Code Snippets
Community Discussions
Trending Discussions on one-fits-all
QUESTION
I need to detect some stuff within a String that contains, among other things, dates. Now, parsing dates using regex is a known question on SO.
However, the dates in this text are localized. And the app needs to be able to adapt to differently localized dates. Luckily, I can figure out the correct date format for the current locale using DateFormat.getDateInstance(SHORT, locale)
. I can get a date pattern from that. But how do I turn it into a regex, dynamically?
The regex would not need to do in-depth validation of the format (leap years, correct amount of days for a month etc.), I can already be sure that the data is provided in a valid format. The date just needs to be identified (as in, the regex should be able to detect the start and end index of where a date is).
The answers in the linked question all assume the handful of common date formats. But assuming that in this case is a likely cause of getting an edge case that breaks the app in a very non-obvious way. Which is why I'd prefer a dynamically generated regex over a one-fits-all(?) solution.
I can't use DateFormat.parse(...)
, since I have to actually detect the date first, and can't directly extract it.
ANSWER
Answered 2018-Sep-18 at 19:48What you're asking is really complicated, but it's not impossible — just likely many hundreds of lines of code before you're done. I'm really not sure that this is the route you want to go — honestly, if you already know what format the date is in, you should probably just parse()
it — but let's say for the sake of argument that you really do want to turn a date pattern like YYYY-mm-dd HH:mm:ss
into a regular expression that can match dates in that format.
There are several steps in the solution: You'll need to lexically analyze the pattern; transform the tokens into correct regex pieces in the current locale; and then mash them all together to make a regex you can use. (Thankfully, you don't need to perform complex parsing on the date-pattern string; lexical analysis is good enough for this.)
Lexical analysis or tokenization is the act of breaking the input string into its component tokens, so that instead of an array of characters, it becomes a sequence of enumerated values or objects: So for the previous example, you'd end up with an array or list like this: [YYYY, Hyphen, mm, Hyphen, dd, Space, HH, Colon, mm, Colon, ss]
. This kind of tokenization is often done with a big state machine, and you may be able to find some open-source code somewhere (part of the Android source code, maybe?) that already does it. If not, you'll have to read each letter, count up how many of that letter there is, and choose an appropriate enum value to add to the growing list of tokens.
Once you have the tokenized sequence of elements, the next step is to transform each into a chunk of a regular expression that is valid for the current localization. This is probably a giant switch
statement inside a loop over the tokens, and thus would turn a YYYY
enum value into the string piece "[0-9]{4}"
, or the mmm
enum value into a big chunk of regex string that matches all of the month names in the current locale ("jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec"
). This obviously involves you pulling all the data for the given locale, so that you can make regex chunks out of its words.
Finally, you can concatenate all of the regex bits together, wrapping each bit in parentheses to ensure precedence is correct, and then finally Pattern.compile()
the whole string. Don't forget to make it use a case-insensitive test.
If you don't know what locale you're in, you'll have to do this many times to produce many regexes for each possible locale, and then test the input against each one of them in turn.
This is a project-and-a-half, but it is something that could be built, if you really really really need it to work exactly like you described.
But again, if I were you, I'd stick with something that already exists: If you already know what locale you're in (or even if you don't), the parse()
method already not only does the lexical analysis and input-validation for you — and is not only already written! — but it also produces a usable date object, too!
QUESTION
I am trying to implement things like Firebase in my app, that are asynchronous.
Now I can write an interface callback like this:
...ANSWER
Answered 2017-Mar-18 at 19:58Your answer isn't that specific to libGDX in the fact that a "one-fits-all" callback is what you get by moving into Java 8 with some cool features borrowed from functional programming like Lambda expressions that allow you to manipulate functions (including callbacks) in a much nicer way. I'd recommend reading up on learning how to use the Java 8 interface if possible.
If, however, Java 8 isn't an option (e.g. for backwards compatibility) you can very easily roll up your own generic callback class and then use that within your code.
This way you won't create a new interface for each type but, this won't be as flexible as lambda expressions because you still need to create an interface for each number of arguments.
E.g.
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install one-fits-all
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page