@Override
        public String calculateValue(QueryOptions queryOptions) {
            Set<Group> groups = new LinkedHashSet<>();
            for (InheritanceNode node : this.user.getOwnInheritanceNodes(queryOptions)) {
                Group group = this.user.getPlugin().getGroupManager().getIfLoaded(node.getGroupName());
                if (group != null) {
                    groups.add(group);
                }
            }

            Group bestGroup = null;
            int best = 0;

            for (Group g : groups) {
                int weight = g.getWeight().orElse(0);
                if (bestGroup == null || weight > best) {
                    bestGroup = g;
                    best = weight;
                }
            }

            return bestGroup == null ? super.calculateValue(queryOptions) : bestGroup.getName();
        }
Simple implementation which just holds a stored value.
common/src/main/java/me/lucko/luckperms/common/model/PrimaryGroupHolder.java