sentence
stringlengths 1
1.38k
| label
stringclasses 3
values |
---|---|
I would suggest creating a thin logging facade that can write to any of the logging frameworks, at which point the choice of backing engine become pretty much a moot point.
|
o
|
That is what commons logging does so why reinvent the wheel?
|
o
|
Also there are some issues that your facade would have to handle that commons logging already does.
|
o
|
+1 To counter commons logging argument, Some enterprise apps use custom logger.
|
o
|
It always a good idea to hide underlying implementation.
|
p
|
The 'thin' wrapper eliminates the need for another jar being packaged and is not as much as reinventing.
|
o
|
This approach is valid in many cases, especially when your code must run on an arbitrary platform.
|
o
|
The underlying logging API tends to be different on application servers compared to Eclipse, for example.
|
o
|
About hiding the underlying implementation: All the logging APIs mentioned can do just that: They are interfaces to pluggable implementations.
|
o
|
This saved me recently when we ported our framework to the Compact Framework (in .Net).
|
o
|
If we had hardcoded dependencies on nLog, we would have been screwed.
|
n
|
Because we used this approach, we were able to drop in a null logger and be happy.Someone vote me back up to 0 Please :-).
|
o
|
One already exists - take a look at slf4j.
|
o
|
It's an accepted thin wrapper that allows you to switch logging frameworks on application startup by switching jars on the runtime classpath.
|
o
|
clogging has its own way of discovering what framework it will use - its not nice and rather convuluted.
|
o
|
How many logging frameworks has Ceki created.
|
o
|
One should always strive to slot a level of interdirection and hide an implementation even if it is clogging with your own logging.
|
o
|
Behind the scenes you can then plugin whatever f/w you desire as mentioned by Travis.
|
o
|
Java - Create a new String instance with specified length and filled with specific character.
|
o
|
Best solution?
|
o
|
I did check the other questions; this question has its focus on solving this particular question the most efficient way.
|
o
|
Sometimes you want to create a new string with a specified length, and with a default character filling the entire string.
|
o
|
ie, it would be cool if you could do CODETERM1 and create a new String from there, with a length of 10 characters all having a .
|
p
|
Because such a constructor does not exist, and you cannot extend from String, you have either to create a wrapper class or a method to do this for you.
|
o
|
At this moment I am using this: CODESNIPPET_JAVA1 .
|
o
|
It still lacks any checking (ie, when length is 0 it will not work).
|
n
|
I am constructing the array first because I believe it is faster than using string concatination or using a StringBuffer to do so.
|
p
|
Anyone else has a better sollution?
|
o
|
Simply use the StringUtils class from URL_http://commons.apache.org/lang [apache-commons-lang] project.
|
o
|
You have a URL_http://commons.apache.org/lang// api-2.4/org/apache/commons/lang/StringUtils.html#leftPad%28java.lang.String,%2 0int,%20char%29 [leftPad] method: CODESNIPPET_JAVA1 .
|
o
|
+1 - that's exactly what he was looking for (aka 'cool').
|
o
|
For the exact same behaviour, but that's obvious, do: StringUtils.leftPad("", 10, '*');.
|
o
|
+1 nice solution, though I wouldn't add a jar to my project just for that.
|
p
|
Thanks.
|
o
|
Yes that looks like a more suitable way to use it.
|
o
|
Luckily I am already using this jar, so i could use this straight away.
|
p
|
Or use repeat(), which doesn't require the empty string at the start and is arguably clearer in intent (see my answer).
|
p
|
@abyx> Yes, you will need to add a JAR, but commons lang offers so nice utils methods, so I don't think it is really a drawback!.
|
p
|
Over 300kb for this jar??
|
o
|
Yuck, no thanks.
|
o
|
Not for a mobile app.
|
o
|
No need for bloatware.
|
o
|
No need to do the loop, and using just standard Java library classes: CODESNIPPET_JAVA1 .
|
p
|
As you can see, I also added suitable code for the CODETERM1 case.
|
o
|
won't the code in Arrays.fill() contain a loop...
|
o
|
Maybe, but 1) it's likely to be faster and 2) that's code you don't need to write.
|
p
|
Good find.
|
p
|
Yes Arrays.fill does a for loop itself.
|
o
|
But fundementally this is better.
|
o
|
You should throw an IllegalArgumentException if (length < 0).
|
o
|
(Even java.lang.NegativeArraySizeException is better.
|
o
|
).
|
o
|
Apache Commons Lang (probably useful enough to be on the classpath of any non- trivial project) has URL_http://commons.apache.org/lang//api-2.4/org/apache/co mmons/lang/StringUtils.html#repeat%28java.lang.String,%20int%29 [StringUtils.repeat()] : CODESNIPPET_JAVA1 .
|
n
|
Easy!
|
p
|
CODESNIPPET_JAVA1 .
|
o
|
What is wrong?
|
o
|
The above is fine.
|
o
|
Do you mind if I ask you a question - Is this causing you a problem?
|
o
|
It seams to me you are optimizing before you know if you need to.
|
o
|
Now for my over engineered solution.
|
o
|
In many (thou not all) cases you can use CharSequence instead of a String.
|
o
|
CODESNIPPET_JAVA1 .
|
o
|
CODESNIPPET_JAVA1 .
|
o
|
One extra note: it seems that all public ways of creating a new CODETERM1 instance involves necessarily the copy of whatever buffer you are working with, be it a CODETERM2 , a CODETERM3 or a CODETERM4 .
|
p
|
From the CODETERM5 javadoc (and is repeated in the respective CODETERM6 methods from the other classes):
_The contents of the character array are copied; subsequent modification of the character array does not affect the newly created string._ So you'll end up having a possibly big memory copy operation after the "fast filling" of the array.
|
n
|
The only solution that may avoid this issue is the one from @mlk, if you can manage working directly with the proposed CODETERM7 implementation (what may be the case).
|
p
|
PS: I would post this as a comment but I don't have enough reputation to do that yet.
|
p
|
using URL_http://bitbucket.org/dfa/dollar [Dollar] is simple: CODESNIPPET_JAVA1 .
|
p
|
There are 9
in the comment.
|
o
|
To improve performance you could have a single predefined sting if you know the max length like:
String template
"####################################"; And then simply perform a substring once you know the length.
|
n
|
Karl .
|
o
|
+1: I just read on another post that substring creates a String that just uses the same char[] of the original String.
|
o
|
So it's an O(1) operation (no loop) and it saves memory (if that is a real issue).
|
n
|
Try this Using the substring(int start, int end); method CODESNIPPET_JAVA1 .
|
o
|
this will return abcde.
|
o
|
C# equivalent to Java's charAt()?
|
o
|
I know we can use the CODETERM1 method in Java get an individual character in a string by specifying its position.
|
o
|
Is there an equivalent method in C#?
|
o
|
You can index into a string in C# like an array, and you get the character at that index.
|
o
|
Example: In Java, you would say str.charAt(8);In C#, you would say str[8]; .
|
o
|
CODESNIPPET_JAVA1 .
|
o
|
And <strike> CODETERM1 Reference: URL_http://msdn.microsoft.com/en- us/library/system.string.chars%28v=VS.71%29.aspx [ URL_http://msdn.microsoft.com /en-us/library/system.string.chars%28v=VS.71%29.aspx] </strike> The above is same as using indexers in c#.
|
o
|
You beat me to it -- you get the upvote.
|
p
|
I really don't know if it helps anything at all but I'll be damned if I don't try.
|
n
|
I had a similar problem when I required a specific position/index, and LINQ Saved the day actually, I just wish I remembered where I put that blasted code.
|
o
|
Sorry for the crappy answer, but it might be a good starting point if ALL ELSE fails.
|
n
|
Console.WriteLine allows the user to specify a position in a string.
|
o
|
See sample: string str
"Tigger";Console.WriteLine( str[0] ); //returns "T";Console.WriteLine( str[2] ); //returns "g"; There you go!
|
o
|
this topic, and your answer, have nothing to do with WriteLine...
|
o
|
String, StringBuffer, and StringBuilder.
|
o
|
Please tell me a real time situation to compare CODETERM1 , CODETERM2 , and CODETERM3 ?
|
o
|
Difference:** CODETERM1 is
if you try to alter their values, another object gets created, whereas CODETERM2 and CODETERM3 are
so they can change their values.
|
o
|
Difference:** The difference between CODETERM4 and CODETERM5 is that CODETERM6 is thread-safe.
|
p
|
So when the application needs to be run only in a single thread then it is better to use CODETERM7 .
|
o
|
CODETERM8 is more efficient than CODETERM9 .
|
p
|
If your string is not going to change use a String class because a CODETERM10 object is immutable.
|
o
|
If your string can change (example: lots of logic and operations in the construction of the string) and will only be accessed from a single thread, using a CODETERM11 is good enough.
|
p
|
If your string can change, and will be accessed from multiple threads, use a CODETERM12 because CODETERM13 is synchronous so you have thread-safety.
|
p
|
Also, using String for logic operations is rather slow, and is not advised at all, since the JVM converts the String to a StringBuffer in the bytecode.
|
n
|
A lot of overhead is wasted converting from String to StringBuffer and then back to String again.
|
o
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.