View Javadoc
1   /*
2    * Copyright 2022-2026 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    *      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  package org.bremersee.acl.spring.data.mongodb.convert;
18  
19  import java.util.List;
20  import java.util.Objects;
21  import org.bremersee.acl.Ace;
22  import org.bson.Document;
23  import org.jspecify.annotations.NonNull;
24  import org.springframework.core.convert.converter.Converter;
25  import org.springframework.data.convert.ReadingConverter;
26  
27  /**
28   * The document to ace converter.
29   *
30   * @author Christian Bremer
31   */
32  @ReadingConverter
33  public class DocumentToAceConverter implements Converter<Document, Ace> {
34  
35    /**
36     * Instantiates a new document to ace converter.
37     */
38    public DocumentToAceConverter() {
39      super();
40    }
41  
42    @Override
43    public Ace convert(@NonNull Document source) {
44      return Ace.builder()
45          .guest(source.getBoolean(Ace.GUEST, false))
46          .users(source.getList(Ace.USERS, String.class, List.of()))
47          .roles(source.getList(Ace.ROLES, String.class, List.of()))
48          .groups(source.getList(Ace.GROUPS, String.class, List.of()))
49          .build();
50    }
51  
52    @Override
53    public boolean equals(Object o) {
54      if (this == o) {
55        return true;
56      }
57      return o != null && getClass() == o.getClass();
58    }
59  
60    @Override
61    public int hashCode() {
62      return Objects.hashCode(getClass());
63    }
64  }