Advertisement
afinlay

Unsuccessful Override of List.add(index, element)

Mar 24th, 2018
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.52 KB | None | 0 0
  1. """
  2. Copyright (C) 2018 Adrian D. Finlay. All rights reserved.
  3.  
  4. Licensed under the MIT License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7.  
  8. https://opensource.org/licenses/MIT
  9.  
  10. Permission is hereby granted, free of charge, to any person obtaining a copy
  11. of this software and associated documentation files (the "Software"), to deal
  12. in the Software without restriction, including without limitation the rights
  13. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  14. copies of the Software, and to permit persons to whom the Software is
  15. furnished to do so, subject to the following conditions:
  16.  
  17. The above copyright notice and this permission notice shall be included in all
  18. copies or substantial portions of the Software.
  19.  
  20. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  21. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  22. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  23. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  24. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  25. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  26. SOFTWARE.
  27. ==============================================================================
  28. """
  29.  
  30. from java.util import ArrayList #java.util.ArrayList
  31. from java.time import LocalDateTime #java.time.LocalDateTime
  32. from java.util import UUID #java.util.UUID
  33.  
  34. class HistoryDataElement(ArrayList):
  35. #Initializer
  36. def __init__(self,uuid,url,title,dateTime):
  37.  
  38. '''HistoryDataElements must have at least one URL, Title, & DateTime '''
  39. valid = (type(uuid) is UUID) and (isinstance(url,str)) and (isinstance(title,str)) and (type(dateTime) is LocalDateTime)
  40.  
  41. if not valid:
  42. from InvalidHistoryDataElementException import InvalidHistoryDataElementException
  43. raise InvalidHistoryDataElementException()
  44. sys.exit()
  45. else:
  46. super(HistoryDataElement,self).add(0, uuid)
  47. super(HistoryDataElement,self).add(1, url)
  48. super(HistoryDataElement,self).add(2, title)
  49. super(HistoryDataElement,self).add(3, dateTime)
  50.  
  51. #Utility Methods
  52. def getTitle(self):
  53. print(self.get(0))
  54. return self.get(0)
  55. def getURL(self):
  56. print(self.get(1))
  57. return self.get(1)
  58. def getDateTime (self):
  59. print(self.get(2))
  60. return self.get(2)
  61. def getUUID(self):
  62. print(self.get(2))
  63. return self.get(3)
  64.  
  65. # @Override of java.lang.ArrayList's inherited add(T)
  66. def add(self,T):
  67. if type(T) is not LocalDateTime:
  68. print("You may only add another visit (java.time.LocalDateTime) to an existing URL.")
  69. else:
  70. ArrayList.add(self, T)
  71. #@Override
  72. def add(self, index, element):
  73. raise InvalidSHEOperationException("add(int index, E element)")
  74. #@Override
  75. def addAll(self, collection):
  76. raise InvalidSHEOperationException("addAll(Collection<? extends E> c)")
  77. #@Override
  78. def addAll(self, index, collection):
  79. raise InvalidSHEOperationException("addAll(int index, Collection<? extends E> c)")
  80. #@Override
  81. def clear(self):
  82. raise InvalidSHEOperationException("clear()")
  83. #@Override
  84. def isEmpty(self):
  85. print('SessionHistoryElement will always be of size: 3')
  86. #@Override
  87. def remove(self, E):
  88. raise InvalidSHEOperationException("remove(int index) or remove(Object o)")
  89. #@Override
  90. def removeAll(self, collection):
  91. raise InvalidSHEOperationException("removeAll(Collection<?> c)")
  92. #@Override
  93. def removeIF(self, filter):
  94. raise InvalidSHEOperationException("removeIf(Predicate<? super E> filter)")
  95. #@Override
  96. def removeRange(self, _from, to):
  97. raise InvalidSHEOperationException("removeRange(int fromIndex, int toIndex)")
  98. #@Override
  99. def replaceAll(self, op):
  100. raise InvalidSHEOperationException("replaceAll(UnaryOperator<E> operator)")
  101. #@Override
  102. def retainAll(self,collection):
  103. raise InvalidSHEOperationException("retainAll(Collection<?> c)")
  104. #@Override
  105. def set (self,index, e):
  106. raise InvalidSHEOperationException("set(int index, E element)")
  107. #Override
  108. def sort(self, comparator):
  109. raise InvalidSHEOperationException("sort(Comparator<? super E> c)")
  110.  
  111. #Test
  112. hde = HistoryDataElement(UUID.randomUUID(), "http://www.adriandavid.me/", "Adrian David | Homepage", LocalDateTime.now())
  113. #hde = HistoryDataElement("fail", "http://www.adriandavid.me/", "Adrian David | Homepage", LocalDateTime.now())
  114. #hde.add(9)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement