/stock/Market.java

https://gitlab.com/wrafter/OMXInvest · Java · 150 lines · 116 code · 19 blank · 15 comment · 14 complexity · 7b5305285f687ca4b83d5acb164ff110 MD5 · raw file

  1. /*
  2. * Copyright © 2011, Simon Wrafter <simon.wrafter@gmail.com>
  3. *
  4. * Permission to use, copy, modify, and/or distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. package stock;
  17. import java.io.IOException;
  18. import java.io.Serializable;
  19. import java.util.SortedMap;
  20. import java.util.TreeMap;
  21. import javax.xml.parsers.DocumentBuilder;
  22. import javax.xml.parsers.DocumentBuilderFactory;
  23. import javax.xml.parsers.ParserConfigurationException;
  24. import org.w3c.dom.Document;
  25. import org.w3c.dom.Element;
  26. import org.w3c.dom.NodeList;
  27. import org.xml.sax.SAXException;
  28. public class Market implements Comparable<Market>, Serializable {
  29. private static final long serialVersionUID = 1283431577549478467L;
  30. private SortedMap<String, Stock> availableStocks;
  31. private String listName;
  32. public Market(String market, String capital)
  33. throws ParserConfigurationException, SAXException, IOException {
  34. int[] i = MarketData.marketIndex(market, capital);
  35. listName = MarketData.arrayMarkets[i[0]] + " " + MarketData.arrayCapital[i[1]];
  36. availableStocks = new TreeMap<String, Stock>();
  37. buildStockMap(market, capital);
  38. }
  39. public String getName() {
  40. return listName;
  41. }
  42. public SortedMap<String, Stock> getMarketMap() {
  43. return new TreeMap<String, Stock>(availableStocks);
  44. }
  45. public void updateMarket(String market, String cap)
  46. throws ParserConfigurationException, SAXException, IOException {
  47. int[] index = MarketData.marketIndex(market, cap);
  48. DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
  49. boolean success = false;
  50. Document doc = null;
  51. int times = 0;
  52. while (!success && times <= 5) {
  53. try {
  54. doc = db.parse(MarketData.buildListURL(index[0], index[1]));
  55. success = true;
  56. } catch (Exception e) {
  57. System.out.println(e.getMessage());
  58. }
  59. times += 1;
  60. }
  61. NodeList nl = doc.getElementsByTagName("inst");
  62. for (int i=0; i<nl.getLength(); i++) {
  63. Element e = (Element) nl.item(i);
  64. if (!availableStocks.containsKey(e.getAttribute("id")))
  65. availableStocks.put(e.getAttribute("id"), buildStock(e));
  66. }
  67. }
  68. public boolean contains(String omxId) {
  69. if (!availableStocks.isEmpty() && omxId.substring(0, 3).equals(availableStocks.firstKey().substring(0, 3)))
  70. return availableStocks.containsKey(omxId);
  71. return false;
  72. }
  73. public boolean rebuildHistory(String omxId)
  74. throws IOException, ParserConfigurationException, SAXException {
  75. Stock s = getStock(omxId);
  76. if (s == null) { return false; }
  77. s.rebuildHistory();
  78. return true;
  79. }
  80. public boolean updateHistory(String omxId)
  81. throws IOException, ParserConfigurationException, SAXException {
  82. Stock s = getStock(omxId);
  83. if (s == null) { return false; }
  84. s.updateHistory();
  85. return true;
  86. }
  87. public Stock getStock(String omxId) {
  88. if (omxId.substring(0, 3).equals(availableStocks.firstKey().substring(0, 3)))
  89. return availableStocks.get(omxId);
  90. return null;
  91. }
  92. private void buildStockMap(String market, String cap)
  93. throws ParserConfigurationException, SAXException, IOException {
  94. int[] index = MarketData.marketIndex(market, cap);
  95. DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
  96. boolean success = false;
  97. Document doc = null;
  98. int times = 0;
  99. while (!success && times <= 5) {
  100. try {
  101. doc = db.parse(MarketData.buildListURL(index[0], index[1]));
  102. success = true;
  103. } catch (Exception e) {
  104. System.out.println(e.getMessage());
  105. }
  106. times += 1;
  107. }
  108. NodeList nl = doc.getElementsByTagName("inst");
  109. for (int i=0; i<nl.getLength(); i++) {
  110. Element e = (Element) nl.item(i);
  111. availableStocks.put(e.getAttribute("id"), buildStock(e));
  112. }
  113. }
  114. private Stock buildStock(Element e) {
  115. return new Stock(
  116. e.getAttribute("id"),
  117. e.getAttribute("nm"),
  118. e.getAttribute("fnm"),
  119. e.getAttribute("isin"),
  120. listName,
  121. Currency.getCurrency(e.getAttribute("cr")));
  122. }
  123. @Override
  124. public int compareTo(Market o) {
  125. return listName.compareTo(o.listName);
  126. }
  127. @Override
  128. public String toString() {
  129. return listName;
  130. }
  131. }