/platform/lang-impl/src/com/intellij/psi/stubs/SerializedStubTree.java

https://bitbucket.org/nbargnesi/idea · Java · 98 lines · 62 code · 15 blank · 21 comment · 14 complexity · 18296c4f97c0abd941797d175198c25f MD5 · raw file

  1. /*
  2. * Copyright 2000-2009 JetBrains s.r.o.
  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. * http://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. /*
  17. * @author max
  18. */
  19. package com.intellij.psi.stubs;
  20. import com.intellij.util.CompressionUtil;
  21. import com.intellij.util.io.UnsyncByteArrayInputStream;
  22. import org.jetbrains.annotations.Nullable;
  23. import java.io.DataInput;
  24. import java.io.DataOutput;
  25. import java.io.IOException;
  26. public class SerializedStubTree {
  27. private final byte[] myBytes;
  28. private final int myLength;
  29. private Stub myStubElement;
  30. public SerializedStubTree(final byte[] bytes, int length, @Nullable Stub stubElement) {
  31. myBytes = bytes;
  32. myLength = length;
  33. myStubElement = stubElement;
  34. }
  35. public SerializedStubTree(DataInput in) throws IOException {
  36. myBytes = CompressionUtil.readCompressed(in);
  37. myLength = myBytes.length;
  38. }
  39. public void write(DataOutput out) throws IOException {
  40. CompressionUtil.writeCompressed(out, myBytes, myLength);
  41. }
  42. // willIndexStub is one time optimization hint, once can safely pass false
  43. public Stub getStub(boolean willIndexStub) throws SerializerNotFoundException {
  44. Stub stubElement = myStubElement;
  45. if (stubElement != null) {
  46. // not null myStubElement means we just built SerializedStubTree for indexing,
  47. // if we request stub for indexing we can safely use it
  48. myStubElement = null;
  49. if (willIndexStub) return stubElement;
  50. }
  51. return SerializationManagerEx.getInstanceEx().deserialize(new UnsyncByteArrayInputStream(myBytes));
  52. }
  53. public boolean equals(final Object that) {
  54. if (this == that) {
  55. return true;
  56. }
  57. if (!(that instanceof SerializedStubTree)) {
  58. return false;
  59. }
  60. final SerializedStubTree thatTree = (SerializedStubTree)that;
  61. final int length = myLength;
  62. if (length != thatTree.myLength) {
  63. return false;
  64. }
  65. final byte[] thisBytes = myBytes;
  66. final byte[] thatBytes = thatTree.myBytes;
  67. for (int i=0; i< length; i++) {
  68. if (thisBytes[i] != thatBytes[i]) {
  69. return false;
  70. }
  71. }
  72. return true;
  73. }
  74. public int hashCode() {
  75. if (myBytes == null)
  76. return 0;
  77. int result = 1;
  78. for (int i = 0; i < myLength; i++) {
  79. result = 31 * result + myBytes[i];
  80. }
  81. return result;
  82. }
  83. }