PageRenderTime 42ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/spel/ExpressionTransformationContextSupport.java

http://github.com/SpringSource/spring-data-mongodb
Java | 134 lines | 45 code | 17 blank | 72 comment | 4 complexity | b07ccc2d540a5b073e6f0b2b50fe8093 MD5 | raw file
  1. /*
  2. * Copyright 2013-2021 the original author or authors.
  3. *
  4. * Licensed under the Apache 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://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.springframework.data.mongodb.core.spel;
  17. import java.util.List;
  18. import org.bson.Document;
  19. import org.springframework.lang.Nullable;
  20. import org.springframework.util.Assert;
  21. /**
  22. * The context for an {@link ExpressionNode} transformation.
  23. *
  24. * @author Thomas Darimont
  25. * @author Oliver Gierke
  26. * @author Christoph Strobl
  27. * @author Mark Paluch
  28. */
  29. public class ExpressionTransformationContextSupport<T extends ExpressionNode> {
  30. private final T currentNode;
  31. private final @Nullable ExpressionNode parentNode;
  32. private final @Nullable Document previousOperationObject;
  33. /**
  34. * Creates a new {@link ExpressionTransformationContextSupport} for the given {@link ExpressionNode}s and an optional
  35. * previous operation.
  36. *
  37. * @param currentNode must not be {@literal null}.
  38. * @param parentNode may be {@literal null}.
  39. * @param previousOperationObject may be {@literal null}.
  40. */
  41. public ExpressionTransformationContextSupport(T currentNode, @Nullable ExpressionNode parentNode,
  42. @Nullable Document previousOperationObject) {
  43. Assert.notNull(currentNode, "currentNode must not be null!");
  44. this.currentNode = currentNode;
  45. this.parentNode = parentNode;
  46. this.previousOperationObject = previousOperationObject;
  47. }
  48. /**
  49. * Returns the current {@link ExpressionNode}.
  50. *
  51. * @return
  52. */
  53. public T getCurrentNode() {
  54. return currentNode;
  55. }
  56. /**
  57. * Returns the parent {@link ExpressionNode} or {@literal null} if none available.
  58. *
  59. * @return
  60. */
  61. @Nullable
  62. public ExpressionNode getParentNode() {
  63. return parentNode;
  64. }
  65. /**
  66. * Returns the previously accumulated operation object or {@literal null} if none available. Rather than manually
  67. * adding stuff to the object prefer using {@link #addToPreviousOrReturn(Object)} to transparently do if one is
  68. * present.
  69. *
  70. * @see #hasPreviousOperation()
  71. * @see #addToPreviousOrReturn(Object)
  72. * @return
  73. */
  74. @Nullable
  75. public Document getPreviousOperationObject() {
  76. return previousOperationObject;
  77. }
  78. /**
  79. * Returns whether a previous operation is present.
  80. *
  81. * @return
  82. */
  83. public boolean hasPreviousOperation() {
  84. return getPreviousOperationObject() != null;
  85. }
  86. /**
  87. * Returns whether the parent node is of the same operation as the current node.
  88. *
  89. * @return
  90. */
  91. public boolean parentIsSameOperation() {
  92. return parentNode != null && currentNode.isOfSameTypeAs(parentNode);
  93. }
  94. /**
  95. * Adds the given value to the previous operation and returns it.
  96. *
  97. * @param value
  98. * @return
  99. */
  100. public Document addToPreviousOperation(Object value) {
  101. Assert.state(previousOperationObject != null, "No previous operation available!");
  102. extractArgumentListFrom(previousOperationObject).add(value);
  103. return previousOperationObject;
  104. }
  105. /**
  106. * Adds the given value to the previous operation if one is present or returns the value to add as is.
  107. *
  108. * @param value
  109. * @return
  110. */
  111. public Object addToPreviousOrReturn(Object value) {
  112. return hasPreviousOperation() ? addToPreviousOperation(value) : value;
  113. }
  114. private List<Object> extractArgumentListFrom(Document context) {
  115. return (List<Object>) context.get(context.keySet().iterator().next());
  116. }
  117. }