In a method i'm receiving vk_key's as just a int, nothing more is provided.
I need to forward this, which looks like:
parent.postEvent(new KeyEvent(null, parent.millis(), KeyEvent.TYPE, modiefiers, c_key, vk_key));
The problem is there are also things like VK_UP, VK_DOWN etc.
Where normally I need to do a cast: char c_key = (char)vk_key;, this is incorrect for VK_UP etc. cause the cast will make the up key a '&' for example.
For those cases the c_key should be set to KeyEvent.CHAR_UNDEFINED.
What I do now is this:
switch (vk_key) {
case VK_UP:
case VK_LEFT:
case VK_RIGHT:
case VK_DOWN:
c_key = CHAR_UNDEFINED;
break;
}
But this does not feel like the right way to do it.
Ideally there would have been a static method like static public char getCharForKey(int vk_key), which would return CHAR_UNDEFINED where required. But as always oracle makes things way harder then they should be.
So is there any easy way to figure out if something should be CHAR_UNDEFINED or not?
Related
I inherited some old Cocoon code, if it's obvious, please tell me where in the docs I could find it.
I have a function to clean up some page parameters. I want to hand over the content to that function, but I can't manage to get there.
This is the original code, the value must pass my "cleanPath" function.
<jdbp:page-param name="pageToLoad" value="${{request.requestURI}}/../{#page}"/>
Attempt 1:
<jdbp:page-param name="pageToLoad" value="cleanPath(${{request.requestURI}}/../{#page})"/>
My attempt was to add the function in and leave everything like that, but it's not working.
My next attempt is these nice xsp:logic blocks, where I can't get the requestURI. "request.requestURI" is unknown, evaluate complains
"Type mismatch: cannot convert from Object to String".
<xsp:logic>
String input2 = evaluate("${{request.requestURI}}", String.class);
String input = "/../<xsl:value-of select="#page"/>";
putVariable("Hase","Test "+input);
</xsp:logic>
<jdbp:page-param name="pageToLoad" value="${{request.requestURI}}/../{#page}"/>
<jdbp:page-param name="Hase" value="${{Hase}}"/>
It's easy to just call request.getRequestURI();. No need for complicated wrappers.
I recently got a report that a few Google Analytics event category names were being recorded with an i character with out a dot on top.
Pageviews and events occurring twice, once without dots over the i.
I had to look to believe it. Sure enough, I had an event called favorite and there was a handful called favorıte. Copy and paste that weird character into a terminal or a monospace font just to see how weird it is. favorıte
My first suspicion is my code where I generate the strings for the category names using toString on an enum.
public enum AnalyticsEvent {
SCREEN_VIEW,
FAVORITE,
UN_FAVORITE,
CLICK_EVENT,
... reduced for brevity;
public String val() {
return this.toString().toLowerCase();
}
}
Example of how that enum is used:
#Override
public void logSearchTag(String type, String value) {
...
logGAEvent(AnalyticsEvent.SEARCH_TAG.val(), type, value);
}
private void logGAEvent(String category, String action, String label) {
... // mGATracker = instance of com.google.android.gms.analytics.Tracker;
mGATracker.send(addCustomDimensions(new HitBuilders.EventBuilder()
.setCategory(category)
.setAction(action)
.setLabel(label))
.build());
...
}
I am going to solve this by actually assigning a string to the enums and instead return that in the val() function.
Though, I am curious if anyone knows why on a small handful of devices Enum.toString returns the enum name with that weird character replacing the i. I mean small. 8 out 50,000 is the average. Or is it possible that assumption is wrong and the error is on analytics service end somewhere? Really highly doubt that.
The String#toLowerCase method uses the default locale of the system. This use locale specific characters such as ı instead of i. In order to fix this problem call toLowerCase with a locale:
String test = "testString";
test.toLowerCase(java.util.Locale.ENGLISH) // Or your preferred locale
I defined a EditText in XML with attribute android:inputType="numberSigned", so, when I try to get it in Java Code like:
int type = mEditText.getInputType();
switch(type){
case InputType.TYPE_NUMBER_FLAG_SIGNED:
//do when I get EditText defined with 'numberSinged'
//do something
break;
}
But, It doesn't work for me. So I try to check Android source code, TYPE_NUMBER_FLAG_SIGNED=4096. When I try to print println(mEditText.getInputType()),it turns to be 4098. And I can't find any variable equals 4098.
Can anybody tell me the reason?
I'm not good at English, may you can understand me! Thanks!
there can be multiple flags assigned to inputType. To find out if a flag is set or not, use the bitwise AND (&) operator:
int type = mEditText.getInputType();
if((type & InputType.TYPE_NUMBER_FLAG_SIGNED) > 0)
{
// your stuff here
}
I guess, the usage of switch case is not possible here.
TYPE_NUMBER_FLAG_SIGNED Constant Value: 4096 (0x00001000) .
get more information here
New to Java, and can't figure out what I hope to be a simple thing.
I keep "sections" in an array:
//Section.java
public static final String[] TOP = {
"Top News",
"http://www.mysite.com/RSS/myfeed.csp",
"top"
};
I'd like to do something like this:
Article a1 = new Article();
a1.["s_" + section[2]] = 1; //should resolve to a1.s_top = 1;
But it won't let me, as it doesn't know what "section" is. (I'm sure seasoned Java people will cringe at this attempt... but my searches have come up empty on how to do this)
Clarification:
My article mysqlite table has fields for the "section" of the article:
s_top
s_sports
...etc
When doing my import from an XML file, I'd like to set that field to a 1 if it's in that category. I could have switch statement:
//whatever the Java version of this is
switch(section[2]) {
case "top": a1.s_top = 1; break;
case "sports": a1.s_sports = 1; break;
//...
}
But I thought it'd be a lot easier to just write it as a single line:
a1["s_"+section[2]] = 1;
In Java, it's a pain to do what you want to do in the way that you're trying to do it.
If you don't want to use the switch/case statement, you could use reflection to pull up the member attribute you're trying to set:
Class articleClass = a1.getClass();
Field field = articleClass.getField("s_top");
field.set(a1, 1);
It'll work, but it may be slow and it's an atypical approach to this problem.
Alternately, you could store either a Map<String> or a Map<String,Boolean> inside of your Article class, and have a public function within Article called putSection(String section), and as you iterate, you would put the various section strings (or string/value mappings) into the map for each Article. So, instead of statically defining which sections may exist and giving each Article a yes or no, you'd allow the list of possible sections to be dynamic and based on your xml import.
Java variables are not "dynamic", unlink actionscript for exemple. You cannot call or assign a variable without knowing it at compile time (well, with reflection you could but it's far to complex)
So yes, the solution is to have a switch case (only possible on strings with java 1.7), or using an hashmap or equivalent
Or, if it's about importing XML, maybe you should take a look on JAXB
If you are trying to get an attribute from an object, you need to make sure that you have "getters" and "setters" in your object. You also have to make sure you define Section in your article class.
Something like:
class Article{
String section;
//constructor
public Article(){
};
//set section
public void setSection(Section section){
this.section = section;
}
//get section
public String getSection(){
return this.section;
}
When working with Java from Scala, we have to account for null.
HttpServletRequest getters (getAttribute, getHeader, etc.) for example, all potentially return null.
I know I can manually do a case/match or map operation on each call to an HttpServletRequest method, but that's a bit tedious. Also, method calls like request.getHeader("Accept-Encoding") are a mouthful.
I came up with an enrichment to handle both issues:
class Servlet_Request_Provides_NullSafe_Getters (r: HttpServletRequest) {
def header(s: String) = Option( r.getHeader(s) )
def attrib(s: String) = Option( r.getAttribute(s) )
def remoteHost = Option( r.getRemoteHost )
def accepts = header("Accept-Encoding")
}
implicit def request2Option(r: HttpServletRequest) =
new Servlet_Request_Provides_NullSafe_Getters(r)
1) Is there another/better way than enrich-my-library to achieve the same/similar affect?
2) If this is "the" way to go, what are the performance impacts/risks? In other words, can one get burned by the apparent utility/conveniences of this pattern?
Sorry if this stuff is obvious, only just started enriching the other day, and it really seems quite useful. Just want to make sure I'm applying the pattern in the proper scenarios...
EDIT
#dhg pointed out that Option.apply() and:
def safe[T](v: T) = v match {
case null => None
case x => Some(x)
}
are equivalent, so the getter methods now use Option(f()) instead of my extraneous safe(f()) wrapper
Thanks
As already mentioned in the comments:
def safe[T](v: T) = Option(v)
Option(v) is equivalent to:
v match {
case null => None
case x => Some(x)
}
Also the safe method is unnecessarily public and part of the class. I would suggest simply inlining it.
2) If this is "the" way to go, what are the performance impacts/risks?
Generally adapting Java legacy APIs to utilize Option is a good idea. I do this often with EntityManager.find() that can return null.
Your implicit conversion is also fine. However don't use underscores in class names, Java/Scala naming conventions prefer CamelCase.