You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

FilterCommandRegistry.java 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * Copyright (C) 2016, Matthias Sohn <matthias.sohn@sap.com> and others
  3. *
  4. * This program and the accompanying materials are made available under the
  5. * terms of the Eclipse Distribution License v. 1.0 which is available at
  6. * https://www.eclipse.org/org/documents/edl-v10.php.
  7. *
  8. * SPDX-License-Identifier: BSD-3-Clause
  9. */
  10. package org.eclipse.jgit.attributes;
  11. import java.io.IOException;
  12. import java.io.InputStream;
  13. import java.io.OutputStream;
  14. import java.util.Map;
  15. import java.util.Set;
  16. import java.util.concurrent.ConcurrentHashMap;
  17. import org.eclipse.jgit.lib.Repository;
  18. /**
  19. * Registry for built-in filters
  20. *
  21. * @since 4.6
  22. */
  23. public class FilterCommandRegistry {
  24. private static Map<String, FilterCommandFactory> filterCommandRegistry = new ConcurrentHashMap<>();
  25. /**
  26. * Register a {@link org.eclipse.jgit.attributes.FilterCommandFactory}
  27. * responsible for creating
  28. * {@link org.eclipse.jgit.attributes.FilterCommand}s for a certain command
  29. * name. If the factory f1 is registered for the name "jgit://builtin/x"
  30. * then a call to <code>getCommand("jgit://builtin/x", ...)</code> will call
  31. * <code>f1(...)</code> to create a new instance of
  32. * {@link org.eclipse.jgit.attributes.FilterCommand}
  33. *
  34. * @param filterCommandName
  35. * the command name for which this factory is registered
  36. * @param factory
  37. * the factory responsible for creating
  38. * {@link org.eclipse.jgit.attributes.FilterCommand}s for the
  39. * specified name
  40. * @return the previous factory associated with <tt>commandName</tt>, or
  41. * <tt>null</tt> if there was no mapping for <tt>commandName</tt>
  42. */
  43. public static FilterCommandFactory register(String filterCommandName,
  44. FilterCommandFactory factory) {
  45. return filterCommandRegistry.put(filterCommandName, factory);
  46. }
  47. /**
  48. * Unregister the {@link org.eclipse.jgit.attributes.FilterCommandFactory}
  49. * registered for the given command name
  50. *
  51. * @param filterCommandName
  52. * the FilterCommandFactory's filter command name
  53. * @return the previous factory associated with <tt>filterCommandName</tt>,
  54. * or <tt>null</tt> if there was no mapping for <tt>commandName</tt>
  55. */
  56. public static FilterCommandFactory unregister(String filterCommandName) {
  57. return filterCommandRegistry.remove(filterCommandName);
  58. }
  59. /**
  60. * Check whether any
  61. * {@link org.eclipse.jgit.attributes.FilterCommandFactory} is registered
  62. * for a given command name
  63. *
  64. * @param filterCommandName
  65. * the name for which the registry should be checked
  66. * @return <code>true</code> if any factory was registered for the name
  67. */
  68. public static boolean isRegistered(String filterCommandName) {
  69. return filterCommandRegistry.containsKey(filterCommandName);
  70. }
  71. /**
  72. * Get registered filter commands
  73. *
  74. * @return Set of commandNames for which a
  75. * {@link org.eclipse.jgit.attributes.FilterCommandFactory} is
  76. * registered
  77. */
  78. public static Set<String> getRegisteredFilterCommands() {
  79. return filterCommandRegistry.keySet();
  80. }
  81. /**
  82. * Create a new {@link org.eclipse.jgit.attributes.FilterCommand} for the
  83. * given name. A factory must be registered for the name in advance.
  84. *
  85. * @param filterCommandName
  86. * The name for which a new
  87. * {@link org.eclipse.jgit.attributes.FilterCommand} should be
  88. * created
  89. * @param db
  90. * the repository this command should work on
  91. * @param in
  92. * the {@link java.io.InputStream} this
  93. * {@link org.eclipse.jgit.attributes.FilterCommand} should read
  94. * from
  95. * @param out
  96. * the {@link java.io.OutputStream} this
  97. * {@link org.eclipse.jgit.attributes.FilterCommand} should write
  98. * to
  99. * @return the command if a command could be created or <code>null</code> if
  100. * there was no factory registered for that name
  101. * @throws java.io.IOException
  102. */
  103. public static FilterCommand createFilterCommand(String filterCommandName,
  104. Repository db, InputStream in, OutputStream out)
  105. throws IOException {
  106. FilterCommandFactory cf = filterCommandRegistry.get(filterCommandName);
  107. return (cf == null) ? null : cf.create(db, in, out);
  108. }
  109. }