Categories
discuss

How to use Spring to inject an object in a setter that doesnt follow the Java bean specification?

I am trying to use Spring and wx-xmlrpc together. The problem is that XmlRpcClient has a setConfig() method that doesnt follow the Java Bean spec : the setter and the getter dont use the same Class. So Spring complaints when I have the following context.xml :

<bean id="xmlRpcClient" class="org.apache.xmlrpc.client.XmlRpcClient">
    <property name="config">
        <bean class="org.apache.xmlrpc.client.XmlRpcClientConfigImpl">
            <property name="serverURL" value="http://example.net" />
        </bean>
    </property>
</bean>

It says : Bean property ‘config’ is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?

Is there a way to override that ? I know I could write a specific factory for this bean, but it seems to me that it is not the last time I find this kind of problem. I work a lot with legacy code of dubious quality … Being able to use Spring XML configuration with it would be a great help !

Answer

Write a FactoryBean for that class and have it call the correct setter.

Categories
discuss

Javascript: why does this produce and ugly string??? I would like currency

var total = 0;
$(".amount").each(function () {
    var value = $(this).val();
    value = (value.length < 1) ? 0 : value;
    var tmp = parseFloat(value).toFixed(2);
    total += tmp;
});
$(".total").text(total);

I am trying to loop through some text boxes and sum up their values. This produces a nasty string. What am I missing?? if I put 8 in the first textbox total text ends up as ” 08.000.000.000.00″. What am I doing wrong? I would like to format as currency but if not, at least just a two decimal number. Any pointers?

Answer

.toFixed converts the object from a Number to a String.

Leave the full values in place and only convert using .toFixed at the very end

$(".total").text(total.toFixed(2));

Alternatively, convert the string back to a number.

total = total + + tmp;
Categories
discuss

Android: Is it more efficient to use a text file or an XML file to store static data

I have some reference data in a text file (~5MB) that I want to use with might android application.

The file is of the format:

1|a|This is line 1a
1|b|This is line 1b
2|a|This is line 2a
2|b|This is line 2b
2|c|This is line 2c

What I want to know is the most efficient way (less memory, fast, size etc.) to use this file within my application.

a.) Should I save the file as a raw resource and open and read the whole file whenever I need a certain line.

b.) Should I convert the file to XML and use XPath to query the file when ever I need to look up a value

<!--sample XML -->
<data>
  <line number="1">
     <entry name="a">This is line 1 a</entry>
  </line>
</data>

c.) Should I just copy & paste the whole file as a static string array in the application and use that.

… any other suggestions are welcome.

[EDIT] I will also need to search this file and jump to arbitrary keywords e.g. “line 1a”.

Answer

XML will always take longer to read than simple text or CSV files. What XML gives you in the tradeoff is a highly structured and reliable way of storing and retrieving data. XML files are, as you can see in the examples above, a good 2-3x larger than the data they actually contain.

If you’re sure that you’re never going to run into the “delimiter” character in your simple text file, then that would probably work just fine, purely from a file speed perspective.

Categories
discuss

Concurrent and Blocking Queue in Java

I have the classic problem of a thread pushing events to the incoming queue of a second thread. Only this time, I am very interested about performance. What I want to achieve is:

  • I want concurrent access to the queue, the producer pushing, the receiver poping.
  • When the queue is empty, I want the consumer to block to the queue, waiting for the producer.

My first idea was to use a LinkedBlockingQueue, but I soon realized that it is not concurrent and the performance suffered. On the other hand, I now use a ConcurrentLinkedQueue, but still I am paying the cost of wait() / notify() on each publication. Since the consumer, upon finding an empty queue, does not block, I have to synchronize and wait() on a lock. On the other part, the producer has to get that lock and notify() upon every single publication. The overall result is that I am paying the cost of
sycnhronized (lock) {lock.notify()} in every single publication, even when not needed.

What I guess is needed here, is a queue that is both blocking and concurrent. I imagine a push() operation to work as in ConcurrentLinkedQueue, with an extra notify() to the object when the pushed element is the first in the list. Such a check I consider to already exist in the ConcurrentLinkedQueue, as pushing requires connecting with the next element. Thus, this would be much faster than synchronizing every time on the external lock.

Is something like this available/reasonable?

Answer

I think you can stick to java.util.concurrent.LinkedBlockingQueue regardless of your doubts. It is concurrent. Though, I have no idea about its performance. Probably, other implementation of BlockingQueue will suit you better. There’s not too many of them, so make performance tests and measure.

Categories
discuss

Ordered List Map implementation in Java

I was wondering if there is a class out there that implements both the Map and List interfaces in Java.

I have a data structure that is primarily a Map. I map strings (IDs) to Images. But in a specific part of my code, I need to present the user with all the available IDed Images. The only way to do that so far is to write this:

for (String id : myMap.keySet()) {
    // get the image like this "myMap.get(id)" 
}

So it would be nice to have a class that implements both Map and List so I could simply write:

for (Image img : myMap) {
  // the image is img
}

Does anyone know of such an implementation?

EDIT: After viewing the answers (which are all correct, voted up), I now realize I would also need the map to be sorted. When I say “sorted”, all I mean is that I would like it to have the values in a specific order, one that I would be able to modify. I know this is not the original question, but I just realized that I need that.

EDIT 2: It seems I am indecisive. What I need is an ordered map, not a sorted one. Sorry for the confusion, people.

Answer

If you need your items in a specific order, LinkedHashMap is your friend – it keeps items in insertion order. TreeMap will keep your items in an order defined by either a Comparator you give or a compareTo method of the key.

Source: stackoverflow
Text is available under the Creative Commons Attribution-ShareAlike License; additional terms may apply. By using this site, you agree to the Privacy Policy, and Copyright Policy. Content is available under CC BY-SA 3.0 unless otherwise noted. The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 © No Copyrights, All Questions are retrived from public domain..