Skip to content

Commit

Permalink
[Misc] Remove NPath complexity in livedata-api
Browse files Browse the repository at this point in the history
Refactor a bit the API to introduce an InitializableLiveDataElement
generic interface to be able to use everywhere needed a call to
initialize. Then use that to provide generic methods to initialize what
needs to be in LiveDataMeta.
  • Loading branch information
surli committed Sep 5, 2024
1 parent 257d531 commit c930891
Show file tree
Hide file tree
Showing 12 changed files with 86 additions and 113 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
<name>XWiki Platform - Live Data - API</name>
<properties>
<xwiki.jacoco.instructionRatio>0.69</xwiki.jacoco.instructionRatio>
<checkstyle.suppressions.location>${basedir}/src/checkstyle/checkstyle-suppressions.xml</checkstyle.suppressions.location>
<!-- Name to display by the Extension Manager -->
<xwiki.extension.name>Live Data API</xwiki.extension.name>
</properties>
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* See the NOTICE file distributed with this work for additional
* information regarding copyright ownership.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/

package org.xwiki.livedata;

import org.xwiki.stability.Unstable;

/**
* Helper interface to represents all live data elements that needs initialization.
*
* @version $Id$
* @since 16.8.0RC1
*/
@Unstable
public interface InitializableLiveDataElement
{
/**
* Prevent {@code null} values where it's possible.
*/
default void initialize()
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
* @since 13.0
*/
@JsonInclude(JsonInclude.Include.NON_NULL)
public class LiveDataActionDescriptor extends BaseDescriptor
public class LiveDataActionDescriptor extends BaseDescriptor implements InitializableLiveDataElement
{
/**
* The action pretty name.
Expand Down Expand Up @@ -187,9 +187,7 @@ public void setUrlProperty(String urlProperty)
this.urlProperty = urlProperty;
}

/**
* Prevent {@code null} values where it's possible.
*/
@Override
public void initialize()
{
if (this.icon == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
*/
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
public class LiveDataConfiguration
public class LiveDataConfiguration implements InitializableLiveDataElement
{
private String id;

Expand Down Expand Up @@ -112,9 +112,7 @@ public void setMeta(LiveDataMeta meta)
this.meta = meta;
}

/**
* Prevent {@code null} values where it's possible.
*/
@Override
public void initialize()
{
if (this.query == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
* @since 12.10
*/
@JsonInclude(JsonInclude.Include.NON_NULL)
public class LiveDataEntryDescriptor
public class LiveDataEntryDescriptor implements InitializableLiveDataElement
{
private String idProperty;

Expand All @@ -49,11 +49,4 @@ public void setIdProperty(String idProperty)
{
this.idProperty = idProperty;
}

/**
* Prevent {@code null} values where it's possible.
*/
public void initialize()
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
* @since 12.10
*/
@JsonInclude(JsonInclude.Include.NON_NULL)
public class LiveDataLayoutDescriptor extends BaseDescriptor
public class LiveDataLayoutDescriptor extends BaseDescriptor implements InitializableLiveDataElement
{
private String name;

Expand Down Expand Up @@ -90,9 +90,7 @@ public void setIcon(Map<String, Object> icon)
this.icon = icon;
}

/**
* Prevent {@code null} values where it's possible.
*/
@Override
public void initialize()
{
if (this.icon == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
*/
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
public class LiveDataMeta
public class LiveDataMeta implements InitializableLiveDataElement
{
private Collection<LiveDataLayoutDescriptor> layouts;

Expand Down Expand Up @@ -310,53 +310,44 @@ public void setDescription(String description)
this.description = description;
}

/**
* Prevent {@code null} values where it's possible.
*/
public void initialize()
private <A extends InitializableLiveDataElement> Collection<A> initializeAndCleanUpCollection(Collection<A>
collection)
{
if (this.layouts == null) {
this.layouts = new ArrayList<>();
}
this.layouts.stream().filter(Objects::nonNull).forEach(LiveDataLayoutDescriptor::initialize);

if (this.propertyDescriptors == null) {
this.propertyDescriptors = new ArrayList<>();
Collection<A> result;
if (collection == null) {
result = new ArrayList<>();
} else {
result = collection;
result.stream().filter(Objects::nonNull).forEach(A::initialize);
}
this.propertyDescriptors.stream().filter(Objects::nonNull).forEach(LiveDataPropertyDescriptor::initialize);
return result;
}

if (this.propertyTypes == null) {
this.propertyTypes = new ArrayList<>();
private <A extends InitializableLiveDataElement> A initialize(A descriptor, A newInstance)
{
A result = descriptor;
if (result == null) {
result = newInstance;
}
this.propertyTypes.stream().filter(Objects::nonNull).forEach(LiveDataPropertyDescriptor::initialize);
result.initialize();
return result;
}

if (this.filters == null) {
this.filters = new ArrayList<>();
}
this.filters.stream().filter(Objects::nonNull).forEach(FilterDescriptor::initialize);
@Override
public void initialize()
{
this.layouts = initializeAndCleanUpCollection(this.layouts);
this.propertyDescriptors = initializeAndCleanUpCollection(this.propertyDescriptors);
this.propertyTypes = initializeAndCleanUpCollection(this.propertyTypes);
this.filters = initializeAndCleanUpCollection(this.filters);
this.actions = initializeAndCleanUpCollection(this.actions);

if (this.displayers == null) {
this.displayers = new ArrayList<>();
}

if (this.pagination == null) {
this.pagination = new LiveDataPaginationConfiguration();
}
this.pagination.initialize();

if (this.entryDescriptor == null) {
this.entryDescriptor = new LiveDataEntryDescriptor();
}
this.entryDescriptor.initialize();

if (this.actions == null) {
this.actions = new ArrayList<>();
}
this.actions.stream().filter(Objects::nonNull).forEach(LiveDataActionDescriptor::initialize);

if (this.selection == null) {
this.selection = new LiveDataSelectionConfiguration();
}
this.selection.initialize();
this.pagination = initialize(this.pagination, new LiveDataPaginationConfiguration());
this.entryDescriptor = initialize(this.entryDescriptor, new LiveDataEntryDescriptor());
this.selection = initialize(this.selection, new LiveDataSelectionConfiguration());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
*/
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
public class LiveDataPaginationConfiguration
public class LiveDataPaginationConfiguration implements InitializableLiveDataElement
{
private Integer maxShownPages;

Expand Down Expand Up @@ -157,9 +157,7 @@ public void setShowPageSizeDropdown(Boolean showPageSizeDropdown)
this.showPageSizeDropdown = showPageSizeDropdown;
}

/**
* Prevent {@code null} values where it's possible.
*/
@Override
public void initialize()
{
if (this.maxShownPages == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@
*/
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
public class LiveDataPropertyDescriptor
public class LiveDataPropertyDescriptor implements InitializableLiveDataElement
{
private static final String NAME_CONSTANT = "name";

/**
* Holds the filter configuration.
*/
@JsonInclude(JsonInclude.Include.NON_NULL)
public static class FilterDescriptor extends BaseDescriptor
public static class FilterDescriptor extends BaseDescriptor implements InitializableLiveDataElement
{
private String defaultOperator;

Expand Down Expand Up @@ -123,9 +123,7 @@ public void setDefaultOperator(String defaultOperator)
this.defaultOperator = defaultOperator;
}

/**
* Prevent {@code null} values where it's possible.
*/
@Override
public void initialize()
{
if (this.operators == null) {
Expand Down Expand Up @@ -561,9 +559,7 @@ public void setEditable(Boolean editable)
this.editable = editable;
}

/**
* Prevent {@code null} values where it's possible.
*/
@Override
public void initialize()
{
if (this.visible == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
*/
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
public class LiveDataQuery
public class LiveDataQuery implements InitializableLiveDataElement
{
/**
* Specifies where to take the data from. Represents the "from" clause.
Expand Down Expand Up @@ -490,9 +490,7 @@ public void setSort(List<SortEntry> sort)
this.sort = sort;
}

/**
* Prevent {@code null} values where it's possible.
*/
@Override
public void initialize()
{
if (this.properties == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
* @since 12.10.1
* @since 13.0
*/
public class LiveDataSelectionConfiguration
public class LiveDataSelectionConfiguration implements InitializableLiveDataElement
{
/**
* Specified whether live data entry selection is enabled.
Expand All @@ -50,11 +50,4 @@ public void setEnabled(boolean enabled)
{
this.enabled = enabled;
}

/**
* Prevent {@code null} values where it's possible.
*/
public void initialize()
{
}
}

0 comments on commit c930891

Please sign in to comment.